| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* PCIe driver for Renesas RZ/G3S SoCs |
| 4 |
* |
| 5 |
* Copyright (C) 2025 Renesas Electronics Corp. |
| 6 |
* |
| 7 |
* Based on: |
| 8 |
* drivers/pci/controller/pcie-rcar-host.c |
| 9 |
* Copyright (C) 2009 - 2011 Paul Mundt |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/bitfield.h> |
| 13 |
#include <linux/bitmap.h> |
| 14 |
#include <linux/bitops.h> |
| 15 |
#include <linux/cleanup.h> |
| 16 |
#include <linux/clk.h> |
| 17 |
#include <linux/delay.h> |
| 18 |
#include <linux/iopoll.h> |
| 19 |
#include <linux/interrupt.h> |
| 20 |
#include <linux/irq.h> |
| 21 |
#include <linux/irqchip/chained_irq.h> |
| 22 |
#include <linux/irqchip/irq-msi-lib.h> |
| 23 |
#include <linux/irqdomain.h> |
| 24 |
#include <linux/kernel.h> |
| 25 |
#include <linux/mfd/syscon.h> |
| 26 |
#include <linux/mutex.h> |
| 27 |
#include <linux/msi.h> |
| 28 |
#include <linux/of_irq.h> |
| 29 |
#include <linux/pci.h> |
| 30 |
#include <linux/platform_device.h> |
| 31 |
#include <linux/pm_runtime.h> |
| 32 |
#include <linux/regmap.h> |
| 33 |
#include <linux/reset.h> |
| 34 |
#include <linux/sizes.h> |
| 35 |
#include <linux/slab.h> |
| 36 |
#include <linux/units.h> |
| 37 |
|
| 38 |
#include "../pci.h" |
| 39 |
|
| 40 |
/* AXI registers */ |
| 41 |
#define RZG3S_PCI_REQDATA(id) (0x80 + (id) * 0x4) |
| 42 |
#define RZG3S_PCI_REQRCVDAT 0x8c |
| 43 |
|
| 44 |
#define RZG3S_PCI_REQADR1 0x90 |
| 45 |
#define RZG3S_PCI_REQADR1_BUS GENMASK(31, 24) |
| 46 |
#define RZG3S_PCI_REQADR1_DEV GENMASK(23, 19) |
| 47 |
#define RZG3S_PCI_REQADR1_FUNC GENMASK(18, 16) |
| 48 |
#define RZG3S_PCI_REQADR1_REG GENMASK(11, 0) |
| 49 |
|
| 50 |
#define RZG3S_PCI_REQBE 0x98 |
| 51 |
#define RZG3S_PCI_REQBE_BYTE_EN GENMASK(3, 0) |
| 52 |
|
| 53 |
#define RZG3S_PCI_REQISS 0x9c |
| 54 |
#define RZG3S_PCI_REQISS_MOR_STATUS GENMASK(18, 16) |
| 55 |
#define RZG3S_PCI_REQISS_TR_TYPE GENMASK(11, 8) |
| 56 |
#define RZG3S_PCI_REQISS_TR_TP0_RD FIELD_PREP(RZG3S_PCI_REQISS_TR_TYPE, 0x4) |
| 57 |
#define RZG3S_PCI_REQISS_TR_TP0_WR FIELD_PREP(RZG3S_PCI_REQISS_TR_TYPE, 0x5) |
| 58 |
#define RZG3S_PCI_REQISS_TR_TP1_RD FIELD_PREP(RZG3S_PCI_REQISS_TR_TYPE, 0x6) |
| 59 |
#define RZG3S_PCI_REQISS_TR_TP1_WR FIELD_PREP(RZG3S_PCI_REQISS_TR_TYPE, 0x7) |
| 60 |
#define RZG3S_PCI_REQISS_REQ_ISSUE BIT(0) |
| 61 |
|
| 62 |
#define RZG3S_PCI_MSIRCVWADRL 0x100 |
| 63 |
#define RZG3S_PCI_MSIRCVWADRL_MASK GENMASK(31, 3) |
| 64 |
#define RZG3S_PCI_MSIRCVWADRL_MSG_DATA_ENA BIT(1) |
| 65 |
#define RZG3S_PCI_MSIRCVWADRL_ENA BIT(0) |
| 66 |
|
| 67 |
#define RZG3S_PCI_MSIRCVWADRU 0x104 |
| 68 |
|
| 69 |
#define RZG3S_PCI_MSIRCVWMSKL 0x108 |
| 70 |
#define RZG3S_PCI_MSIRCVWMSKL_MASK GENMASK(31, 2) |
| 71 |
|
| 72 |
#define RZG3S_PCI_PINTRCVIE 0x110 |
| 73 |
#define RZG3S_PCI_PINTRCVIE_INTX(i) BIT(i) |
| 74 |
#define RZG3S_PCI_PINTRCVIE_MSI BIT(4) |
| 75 |
|
| 76 |
/* Register is R/W1C, it doesn't require locking. */ |
| 77 |
#define RZG3S_PCI_PINTRCVIS 0x114 |
| 78 |
#define RZG3S_PCI_PINTRCVIS_INTX(i) BIT(i) |
| 79 |
#define RZG3S_PCI_PINTRCVIS_MSI BIT(4) |
| 80 |
|
| 81 |
#define RZG3S_PCI_MSGRCVIE 0x120 |
| 82 |
#define RZG3S_PCI_MSGRCVIE_MSG_RCV BIT(24) |
| 83 |
|
| 84 |
#define RZG3S_PCI_MSGRCVIS 0x124 |
| 85 |
#define RZG3S_PCI_MSGRCVIS_MRI BIT(24) |
| 86 |
|
| 87 |
#define RZG3S_PCI_PEIE0 0x200 |
| 88 |
|
| 89 |
#define RZG3S_PCI_PEIS0 0x204 |
| 90 |
#define RZG3S_PCI_PEIS0_RX_DLLP_PM_ENTER BIT(12) |
| 91 |
#define RZG3S_PCI_PEIS0_DL_UPDOWN BIT(9) |
| 92 |
|
| 93 |
#define RZG3S_PCI_PEIE1 0x208 |
| 94 |
#define RZG3S_PCI_PEIS1 0x20c |
| 95 |
#define RZG3S_PCI_AMEIS 0x214 |
| 96 |
#define RZG3S_PCI_ASEIS1 0x224 |
| 97 |
|
| 98 |
#define RZG3S_PCI_PCSTAT1 0x408 |
| 99 |
#define RZG3S_PCI_PCSTAT1_LTSSM_STATE GENMASK(14, 10) |
| 100 |
#define RZG3S_PCI_PCSTAT1_DL_DOWN_STS BIT(0) |
| 101 |
|
| 102 |
#define RZG3S_PCI_PCCTRL2 0x410 |
| 103 |
#define RZG3S_PCI_PCCTRL2_LS_CHG GENMASK(9, 8) |
| 104 |
#define RZG3S_PCI_PCCTRL2_LS_CHG_REQ BIT(0) |
| 105 |
|
| 106 |
#define RZG3S_PCI_PCSTAT2 0x414 |
| 107 |
#define RZG3S_PCI_PCSTAT2_LS_CHG_DONE BIT(28) |
| 108 |
#define RZG3S_PCI_PCSTAT2_SDRIRE GENMASK(7, 1) |
| 109 |
|
| 110 |
#define RZG3S_PCI_PERM 0x300 |
| 111 |
#define RZG3S_PCI_PERM_CFG_HWINIT_EN BIT(2) |
| 112 |
#define RZG3S_PCI_PERM_PIPE_PHY_REG_EN BIT(1) |
| 113 |
|
| 114 |
#define RZG3S_PCI_MSIRE(id) (0x600 + (id) * 0x10) |
| 115 |
#define RZG3S_PCI_MSIRE_ENA BIT(0) |
| 116 |
|
| 117 |
#define RZG3S_PCI_MSIRM(id) (0x608 + (id) * 0x10) |
| 118 |
|
| 119 |
/* Register is R/W1C, it doesn't require locking. */ |
| 120 |
#define RZG3S_PCI_MSIRS(id) (0x60c + (id) * 0x10) |
| 121 |
|
| 122 |
#define RZG3S_PCI_AWBASEL(id) (0x1000 + (id) * 0x20) |
| 123 |
#define RZG3S_PCI_AWBASEL_WIN_ENA BIT(0) |
| 124 |
|
| 125 |
#define RZG3S_PCI_AWBASEU(id) (0x1004 + (id) * 0x20) |
| 126 |
#define RZG3S_PCI_AWMASKL(id) (0x1008 + (id) * 0x20) |
| 127 |
#define RZG3S_PCI_AWMASKU(id) (0x100c + (id) * 0x20) |
| 128 |
#define RZG3S_PCI_ADESTL(id) (0x1010 + (id) * 0x20) |
| 129 |
#define RZG3S_PCI_ADESTU(id) (0x1014 + (id) * 0x20) |
| 130 |
|
| 131 |
#define RZG3S_PCI_PWBASEL(id) (0x1100 + (id) * 0x20) |
| 132 |
#define RZG3S_PCI_PWBASEL_ENA BIT(0) |
| 133 |
|
| 134 |
#define RZG3S_PCI_PWBASEU(id) (0x1104 + (id) * 0x20) |
| 135 |
#define RZG3S_PCI_PDESTL(id) (0x1110 + (id) * 0x20) |
| 136 |
#define RZG3S_PCI_PDESTU(id) (0x1114 + (id) * 0x20) |
| 137 |
#define RZG3S_PCI_PWMASKL(id) (0x1108 + (id) * 0x20) |
| 138 |
#define RZG3S_PCI_PWMASKU(id) (0x110c + (id) * 0x20) |
| 139 |
|
| 140 |
/* PHY control registers */ |
| 141 |
#define RZG3S_PCI_PHY_XCFGD(id) (0x2000 + (id) * 0x10) |
| 142 |
#define RZG3S_PCI_PHY_XCFGD_NUM 39 |
| 143 |
|
| 144 |
#define RZG3S_PCI_PHY_XCFGA_CMN(id) (0x2400 + (id) * 0x10) |
| 145 |
#define RZG3S_PCI_PHY_XCFGA_CMN_NUM 16 |
| 146 |
|
| 147 |
#define RZG3S_PCI_PHY_XCFGA_RX(id) (0x2500 + (id) * 0x10) |
| 148 |
#define RZG3S_PCI_PHY_XCFGA_RX_NUM 13 |
| 149 |
|
| 150 |
#define RZG3S_PCI_PHY_XCFGA_TX 0x25d0 |
| 151 |
|
| 152 |
#define RZG3S_PCI_PHY_XCFG_CTRL 0x2a20 |
| 153 |
#define RZG3S_PCI_PHY_XCFG_CTRL_PHYREG_SEL BIT(0) |
| 154 |
|
| 155 |
/* PCIe registers */ |
| 156 |
#define RZG3S_PCI_CFG_BASE 0x6000 |
| 157 |
#define RZG3S_PCI_CFG_BARMSK00L 0xa0 |
| 158 |
#define RZG3S_PCI_CFG_BARMSK00U 0xa4 |
| 159 |
|
| 160 |
#define RZG3S_PCI_CFG_PCIEC 0x60 |
| 161 |
|
| 162 |
/* System controller registers */ |
| 163 |
#define RZG3S_SYS_PCIE_RST_RSM_B 0xd74 |
| 164 |
#define RZG3S_SYS_PCIE_RST_RSM_B_MASK BIT(0) |
| 165 |
|
| 166 |
/* Maximum number of windows */ |
| 167 |
#define RZG3S_MAX_WINDOWS 8 |
| 168 |
|
| 169 |
/* Number of MSI interrupts per register */ |
| 170 |
#define RZG3S_PCI_MSI_INT_PER_REG 32 |
| 171 |
/* The number of MSI interrupts */ |
| 172 |
#define RZG3S_PCI_MSI_INT_NR RZG3S_PCI_MSI_INT_PER_REG |
| 173 |
|
| 174 |
/* Timeouts experimentally determined */ |
| 175 |
#define RZG3S_REQ_ISSUE_TIMEOUT_US 2500 |
| 176 |
|
| 177 |
/** |
| 178 |
* struct rzg3s_pcie_msi - RZ/G3S PCIe MSI data structure |
| 179 |
* @domain: IRQ domain |
| 180 |
* @map: bitmap with the allocated MSIs |
| 181 |
* @dma_addr: address of the allocated MSI window |
| 182 |
* @window_base: base address of the MSI window |
| 183 |
* @pages: allocated pages for MSI window mapping |
| 184 |
* @map_lock: lock for bitmap with the allocated MSIs |
| 185 |
* @irq: MSI interrupt |
| 186 |
*/ |
| 187 |
struct rzg3s_pcie_msi { |
| 188 |
struct irq_domain *domain; |
| 189 |
DECLARE_BITMAP(map, RZG3S_PCI_MSI_INT_NR); |
| 190 |
dma_addr_t dma_addr; |
| 191 |
dma_addr_t window_base; |
| 192 |
unsigned long pages; |
| 193 |
struct mutex map_lock; |
| 194 |
int irq; |
| 195 |
}; |
| 196 |
|
| 197 |
struct rzg3s_pcie_host; |
| 198 |
|
| 199 |
/** |
| 200 |
* struct rzg3s_pcie_soc_data - SoC specific data |
| 201 |
* @init_phy: PHY initialization function |
| 202 |
* @power_resets: array with the resets that need to be de-asserted after |
| 203 |
* power-on |
| 204 |
* @cfg_resets: array with the resets that need to be de-asserted after |
| 205 |
* configuration |
| 206 |
* @num_power_resets: number of power resets |
| 207 |
* @num_cfg_resets: number of configuration resets |
| 208 |
*/ |
| 209 |
struct rzg3s_pcie_soc_data { |
| 210 |
int (*init_phy)(struct rzg3s_pcie_host *host); |
| 211 |
const char * const *power_resets; |
| 212 |
const char * const *cfg_resets; |
| 213 |
u8 num_power_resets; |
| 214 |
u8 num_cfg_resets; |
| 215 |
}; |
| 216 |
|
| 217 |
/** |
| 218 |
* struct rzg3s_pcie_port - RZ/G3S PCIe Root Port data structure |
| 219 |
* @refclk: PCIe reference clock |
| 220 |
* @vendor_id: Vendor ID |
| 221 |
* @device_id: Device ID |
| 222 |
*/ |
| 223 |
struct rzg3s_pcie_port { |
| 224 |
struct clk *refclk; |
| 225 |
u32 vendor_id; |
| 226 |
u32 device_id; |
| 227 |
}; |
| 228 |
|
| 229 |
/** |
| 230 |
* struct rzg3s_pcie_host - RZ/G3S PCIe data structure |
| 231 |
* @axi: base address for AXI registers |
| 232 |
* @pcie: base address for PCIe registers |
| 233 |
* @dev: struct device |
| 234 |
* @power_resets: reset control signals that should be set after power up |
| 235 |
* @cfg_resets: reset control signals that should be set after configuration |
| 236 |
* @sysc: SYSC regmap |
| 237 |
* @intx_domain: INTx IRQ domain |
| 238 |
* @data: SoC specific data |
| 239 |
* @msi: MSI data structure |
| 240 |
* @port: PCIe Root Port |
| 241 |
* @hw_lock: lock for access to the HW resources |
| 242 |
* @intx_irqs: INTx interrupts |
| 243 |
* @max_link_speed: maximum supported link speed |
| 244 |
*/ |
| 245 |
struct rzg3s_pcie_host { |
| 246 |
void __iomem *axi; |
| 247 |
void __iomem *pcie; |
| 248 |
struct device *dev; |
| 249 |
struct reset_control_bulk_data *power_resets; |
| 250 |
struct reset_control_bulk_data *cfg_resets; |
| 251 |
struct regmap *sysc; |
| 252 |
struct irq_domain *intx_domain; |
| 253 |
const struct rzg3s_pcie_soc_data *data; |
| 254 |
struct rzg3s_pcie_msi msi; |
| 255 |
struct rzg3s_pcie_port port; |
| 256 |
raw_spinlock_t hw_lock; |
| 257 |
int intx_irqs[PCI_NUM_INTX]; |
| 258 |
int max_link_speed; |
| 259 |
}; |
| 260 |
|
| 261 |
#define rzg3s_msi_to_host(_msi) container_of(_msi, struct rzg3s_pcie_host, msi) |
| 262 |
|
| 263 |
static void rzg3s_pcie_update_bits(void __iomem *base, u32 offset, u32 mask, |
| 264 |
u32 val) |
| 265 |
{ |
| 266 |
u32 tmp; |
| 267 |
|
| 268 |
tmp = readl_relaxed(base + offset); |
| 269 |
tmp &= ~mask; |
| 270 |
tmp |= val & mask; |
| 271 |
writel_relaxed(tmp, base + offset); |
| 272 |
} |
| 273 |
|
| 274 |
static int rzg3s_pcie_child_issue_request(struct rzg3s_pcie_host *host) |
| 275 |
{ |
| 276 |
u32 val; |
| 277 |
int ret; |
| 278 |
|
| 279 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_REQISS, |
| 280 |
RZG3S_PCI_REQISS_REQ_ISSUE, |
| 281 |
RZG3S_PCI_REQISS_REQ_ISSUE); |
| 282 |
ret = readl_poll_timeout_atomic(host->axi + RZG3S_PCI_REQISS, val, |
| 283 |
!(val & RZG3S_PCI_REQISS_REQ_ISSUE), |
| 284 |
5, RZG3S_REQ_ISSUE_TIMEOUT_US); |
| 285 |
|
| 286 |
if (val & RZG3S_PCI_REQISS_MOR_STATUS) |
| 287 |
return -EIO; |
| 288 |
|
| 289 |
return ret; |
| 290 |
} |
| 291 |
|
| 292 |
static void rzg3s_pcie_child_prepare_bus(struct pci_bus *bus, |
| 293 |
unsigned int devfn, int where) |
| 294 |
{ |
| 295 |
struct rzg3s_pcie_host *host = bus->sysdata; |
| 296 |
unsigned int dev, func, reg; |
| 297 |
|
| 298 |
dev = PCI_SLOT(devfn); |
| 299 |
func = PCI_FUNC(devfn); |
| 300 |
reg = where & ~0x3; |
| 301 |
|
| 302 |
/* Set the destination */ |
| 303 |
writel_relaxed(FIELD_PREP(RZG3S_PCI_REQADR1_BUS, bus->number) | |
| 304 |
FIELD_PREP(RZG3S_PCI_REQADR1_DEV, dev) | |
| 305 |
FIELD_PREP(RZG3S_PCI_REQADR1_FUNC, func) | |
| 306 |
FIELD_PREP(RZG3S_PCI_REQADR1_REG, reg), |
| 307 |
host->axi + RZG3S_PCI_REQADR1); |
| 308 |
|
| 309 |
/* Set byte enable */ |
| 310 |
writel_relaxed(RZG3S_PCI_REQBE_BYTE_EN, host->axi + RZG3S_PCI_REQBE); |
| 311 |
} |
| 312 |
|
| 313 |
static int rzg3s_pcie_child_read_conf(struct rzg3s_pcie_host *host, |
| 314 |
struct pci_bus *bus, unsigned int devfn, |
| 315 |
int where, u32 *data) |
| 316 |
{ |
| 317 |
bool type0 = pci_is_root_bus(bus->parent) ? true : false; |
| 318 |
int ret; |
| 319 |
|
| 320 |
rzg3s_pcie_child_prepare_bus(bus, devfn, where); |
| 321 |
|
| 322 |
/* Set the type of request */ |
| 323 |
writel_relaxed(type0 ? RZG3S_PCI_REQISS_TR_TP0_RD : |
| 324 |
RZG3S_PCI_REQISS_TR_TP1_RD, |
| 325 |
host->axi + RZG3S_PCI_REQISS); |
| 326 |
|
| 327 |
/* Issue the request and wait to finish */ |
| 328 |
ret = rzg3s_pcie_child_issue_request(host); |
| 329 |
if (ret) |
| 330 |
return PCIBIOS_SET_FAILED; |
| 331 |
|
| 332 |
/* Read the data */ |
| 333 |
*data = readl_relaxed(host->axi + RZG3S_PCI_REQRCVDAT); |
| 334 |
|
| 335 |
return PCIBIOS_SUCCESSFUL; |
| 336 |
} |
| 337 |
|
| 338 |
/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ |
| 339 |
static int rzg3s_pcie_child_read(struct pci_bus *bus, unsigned int devfn, |
| 340 |
int where, int size, u32 *val) |
| 341 |
{ |
| 342 |
struct rzg3s_pcie_host *host = bus->sysdata; |
| 343 |
int ret; |
| 344 |
|
| 345 |
ret = rzg3s_pcie_child_read_conf(host, bus, devfn, where, val); |
| 346 |
if (ret != PCIBIOS_SUCCESSFUL) |
| 347 |
return ret; |
| 348 |
|
| 349 |
if (size <= 2) |
| 350 |
*val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 351 |
|
| 352 |
return PCIBIOS_SUCCESSFUL; |
| 353 |
} |
| 354 |
|
| 355 |
static int rzg3s_pcie_child_write_conf(struct rzg3s_pcie_host *host, |
| 356 |
struct pci_bus *bus, unsigned int devfn, |
| 357 |
int where, u32 data) |
| 358 |
{ |
| 359 |
bool type0 = pci_is_root_bus(bus->parent) ? true : false; |
| 360 |
int ret; |
| 361 |
|
| 362 |
rzg3s_pcie_child_prepare_bus(bus, devfn, where); |
| 363 |
|
| 364 |
/* Set the write data */ |
| 365 |
writel_relaxed(0, host->axi + RZG3S_PCI_REQDATA(0)); |
| 366 |
writel_relaxed(0, host->axi + RZG3S_PCI_REQDATA(1)); |
| 367 |
writel_relaxed(data, host->axi + RZG3S_PCI_REQDATA(2)); |
| 368 |
|
| 369 |
/* Set the type of request */ |
| 370 |
writel_relaxed(type0 ? RZG3S_PCI_REQISS_TR_TP0_WR : |
| 371 |
RZG3S_PCI_REQISS_TR_TP1_WR, |
| 372 |
host->axi + RZG3S_PCI_REQISS); |
| 373 |
|
| 374 |
/* Issue the request and wait to finish */ |
| 375 |
ret = rzg3s_pcie_child_issue_request(host); |
| 376 |
if (ret) |
| 377 |
return PCIBIOS_SET_FAILED; |
| 378 |
|
| 379 |
return PCIBIOS_SUCCESSFUL; |
| 380 |
} |
| 381 |
|
| 382 |
/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ |
| 383 |
static int rzg3s_pcie_child_write(struct pci_bus *bus, unsigned int devfn, |
| 384 |
int where, int size, u32 val) |
| 385 |
{ |
| 386 |
struct rzg3s_pcie_host *host = bus->sysdata; |
| 387 |
u32 data, shift; |
| 388 |
int ret; |
| 389 |
|
| 390 |
if (size == 4) |
| 391 |
return rzg3s_pcie_child_write_conf(host, bus, devfn, where, val); |
| 392 |
|
| 393 |
/* |
| 394 |
* Controller does 32 bit accesses. To do byte accesses software need |
| 395 |
* to do read/modify/write. This may have potential side effects. For |
| 396 |
* example, software may perform a 16-bit write. If the hardware only |
| 397 |
* supports 32-bit accesses, we must do a 32-bit read, merge in the 16 |
| 398 |
* bits we intend to write, followed by a 32-bit write. If the 16 bits |
| 399 |
* we *don't* intend to write happen to have any RW1C |
| 400 |
* (write-one-to-clear) bits set, we just inadvertently cleared |
| 401 |
* something we shouldn't have. |
| 402 |
*/ |
| 403 |
if (!bus->unsafe_warn) { |
| 404 |
dev_warn(&bus->dev, "%d-byte config write to %04x:%02x:%02x.%d offset %#x may corrupt adjacent RW1C bits\n", |
| 405 |
size, pci_domain_nr(bus), bus->number, |
| 406 |
PCI_SLOT(devfn), PCI_FUNC(devfn), where); |
| 407 |
bus->unsafe_warn = 1; |
| 408 |
} |
| 409 |
|
| 410 |
ret = rzg3s_pcie_child_read_conf(host, bus, devfn, where, &data); |
| 411 |
if (ret != PCIBIOS_SUCCESSFUL) |
| 412 |
return ret; |
| 413 |
|
| 414 |
if (size == 1) { |
| 415 |
shift = BITS_PER_BYTE * (where & 3); |
| 416 |
data &= ~(0xff << shift); |
| 417 |
data |= ((val & 0xff) << shift); |
| 418 |
} else if (size == 2) { |
| 419 |
shift = BITS_PER_BYTE * (where & 2); |
| 420 |
data &= ~(0xffff << shift); |
| 421 |
data |= ((val & 0xffff) << shift); |
| 422 |
} else { |
| 423 |
data = val; |
| 424 |
} |
| 425 |
|
| 426 |
return rzg3s_pcie_child_write_conf(host, bus, devfn, where, data); |
| 427 |
} |
| 428 |
|
| 429 |
static struct pci_ops rzg3s_pcie_child_ops = { |
| 430 |
.read = rzg3s_pcie_child_read, |
| 431 |
.write = rzg3s_pcie_child_write, |
| 432 |
}; |
| 433 |
|
| 434 |
static void __iomem *rzg3s_pcie_root_map_bus(struct pci_bus *bus, |
| 435 |
unsigned int devfn, int where) |
| 436 |
{ |
| 437 |
struct rzg3s_pcie_host *host = bus->sysdata; |
| 438 |
|
| 439 |
if (devfn) |
| 440 |
return NULL; |
| 441 |
|
| 442 |
return host->pcie + where; |
| 443 |
} |
| 444 |
|
| 445 |
static struct pci_ops rzg3s_pcie_root_ops = { |
| 446 |
.read = pci_generic_config_read, |
| 447 |
.write = pci_generic_config_write, |
| 448 |
.map_bus = rzg3s_pcie_root_map_bus, |
| 449 |
}; |
| 450 |
|
| 451 |
static void rzg3s_pcie_intx_irq_handler(struct irq_desc *desc) |
| 452 |
{ |
| 453 |
struct rzg3s_pcie_host *host = irq_desc_get_handler_data(desc); |
| 454 |
struct irq_chip *chip = irq_desc_get_chip(desc); |
| 455 |
unsigned int irq = irq_desc_get_irq(desc); |
| 456 |
u32 intx = irq - host->intx_irqs[0]; |
| 457 |
|
| 458 |
chained_irq_enter(chip, desc); |
| 459 |
generic_handle_domain_irq(host->intx_domain, intx); |
| 460 |
chained_irq_exit(chip, desc); |
| 461 |
} |
| 462 |
|
| 463 |
static irqreturn_t rzg3s_pcie_msi_irq(int irq, void *data) |
| 464 |
{ |
| 465 |
u8 regs = RZG3S_PCI_MSI_INT_NR / RZG3S_PCI_MSI_INT_PER_REG; |
| 466 |
DECLARE_BITMAP(bitmap, RZG3S_PCI_MSI_INT_NR) = {0}; |
| 467 |
struct rzg3s_pcie_host *host = data; |
| 468 |
struct rzg3s_pcie_msi *msi = &host->msi; |
| 469 |
unsigned long bit; |
| 470 |
u32 status; |
| 471 |
|
| 472 |
status = readl_relaxed(host->axi + RZG3S_PCI_PINTRCVIS); |
| 473 |
if (!(status & RZG3S_PCI_PINTRCVIS_MSI)) |
| 474 |
return IRQ_NONE; |
| 475 |
|
| 476 |
/* Clear the MSI */ |
| 477 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PINTRCVIS, |
| 478 |
RZG3S_PCI_PINTRCVIS_MSI, |
| 479 |
RZG3S_PCI_PINTRCVIS_MSI); |
| 480 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_MSGRCVIS, |
| 481 |
RZG3S_PCI_MSGRCVIS_MRI, RZG3S_PCI_MSGRCVIS_MRI); |
| 482 |
|
| 483 |
for (u8 reg_id = 0; reg_id < regs; reg_id++) { |
| 484 |
status = readl_relaxed(host->axi + RZG3S_PCI_MSIRS(reg_id)); |
| 485 |
bitmap_write(bitmap, status, reg_id * RZG3S_PCI_MSI_INT_PER_REG, |
| 486 |
RZG3S_PCI_MSI_INT_PER_REG); |
| 487 |
} |
| 488 |
|
| 489 |
for_each_set_bit(bit, bitmap, RZG3S_PCI_MSI_INT_NR) { |
| 490 |
int ret; |
| 491 |
|
| 492 |
ret = generic_handle_domain_irq(msi->domain, bit); |
| 493 |
if (ret) { |
| 494 |
u8 reg_bit = bit % RZG3S_PCI_MSI_INT_PER_REG; |
| 495 |
u8 reg_id = bit / RZG3S_PCI_MSI_INT_PER_REG; |
| 496 |
|
| 497 |
/* Unknown MSI, just clear it */ |
| 498 |
writel_relaxed(BIT(reg_bit), |
| 499 |
host->axi + RZG3S_PCI_MSIRS(reg_id)); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
return IRQ_HANDLED; |
| 504 |
} |
| 505 |
|
| 506 |
static void rzg3s_pcie_msi_irq_ack(struct irq_data *d) |
| 507 |
{ |
| 508 |
struct rzg3s_pcie_msi *msi = irq_data_get_irq_chip_data(d); |
| 509 |
struct rzg3s_pcie_host *host = rzg3s_msi_to_host(msi); |
| 510 |
u8 reg_bit = d->hwirq % RZG3S_PCI_MSI_INT_PER_REG; |
| 511 |
u8 reg_id = d->hwirq / RZG3S_PCI_MSI_INT_PER_REG; |
| 512 |
|
| 513 |
writel_relaxed(BIT(reg_bit), host->axi + RZG3S_PCI_MSIRS(reg_id)); |
| 514 |
} |
| 515 |
|
| 516 |
static void rzg3s_pcie_msi_irq_mask(struct irq_data *d) |
| 517 |
{ |
| 518 |
struct rzg3s_pcie_msi *msi = irq_data_get_irq_chip_data(d); |
| 519 |
struct rzg3s_pcie_host *host = rzg3s_msi_to_host(msi); |
| 520 |
u8 reg_bit = d->hwirq % RZG3S_PCI_MSI_INT_PER_REG; |
| 521 |
u8 reg_id = d->hwirq / RZG3S_PCI_MSI_INT_PER_REG; |
| 522 |
|
| 523 |
guard(raw_spinlock_irqsave)(&host->hw_lock); |
| 524 |
|
| 525 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_MSIRM(reg_id), BIT(reg_bit), |
| 526 |
BIT(reg_bit)); |
| 527 |
} |
| 528 |
|
| 529 |
static void rzg3s_pcie_msi_irq_unmask(struct irq_data *d) |
| 530 |
{ |
| 531 |
struct rzg3s_pcie_msi *msi = irq_data_get_irq_chip_data(d); |
| 532 |
struct rzg3s_pcie_host *host = rzg3s_msi_to_host(msi); |
| 533 |
u8 reg_bit = d->hwirq % RZG3S_PCI_MSI_INT_PER_REG; |
| 534 |
u8 reg_id = d->hwirq / RZG3S_PCI_MSI_INT_PER_REG; |
| 535 |
|
| 536 |
guard(raw_spinlock_irqsave)(&host->hw_lock); |
| 537 |
|
| 538 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_MSIRM(reg_id), BIT(reg_bit), |
| 539 |
0); |
| 540 |
} |
| 541 |
|
| 542 |
static void rzg3s_pcie_irq_compose_msi_msg(struct irq_data *data, |
| 543 |
struct msi_msg *msg) |
| 544 |
{ |
| 545 |
struct rzg3s_pcie_msi *msi = irq_data_get_irq_chip_data(data); |
| 546 |
struct rzg3s_pcie_host *host = rzg3s_msi_to_host(msi); |
| 547 |
u32 lo, hi; |
| 548 |
|
| 549 |
/* |
| 550 |
* Enable and msg data enable bits are part of the address lo. Drop |
| 551 |
* them along with the unused bit. |
| 552 |
*/ |
| 553 |
lo = readl_relaxed(host->axi + RZG3S_PCI_MSIRCVWADRL) & |
| 554 |
RZG3S_PCI_MSIRCVWADRL_MASK; |
| 555 |
hi = readl_relaxed(host->axi + RZG3S_PCI_MSIRCVWADRU); |
| 556 |
|
| 557 |
msg->address_lo = lo; |
| 558 |
msg->address_hi = hi; |
| 559 |
msg->data = data->hwirq; |
| 560 |
} |
| 561 |
|
| 562 |
static struct irq_chip rzg3s_pcie_msi_bottom_chip = { |
| 563 |
.name = "rzg3s-pcie-msi", |
| 564 |
.irq_ack = rzg3s_pcie_msi_irq_ack, |
| 565 |
.irq_mask = rzg3s_pcie_msi_irq_mask, |
| 566 |
.irq_unmask = rzg3s_pcie_msi_irq_unmask, |
| 567 |
.irq_compose_msi_msg = rzg3s_pcie_irq_compose_msi_msg, |
| 568 |
}; |
| 569 |
|
| 570 |
static int rzg3s_pcie_msi_domain_alloc(struct irq_domain *domain, |
| 571 |
unsigned int virq, unsigned int nr_irqs, |
| 572 |
void *args) |
| 573 |
{ |
| 574 |
struct rzg3s_pcie_msi *msi = domain->host_data; |
| 575 |
int hwirq; |
| 576 |
|
| 577 |
scoped_guard(mutex, &msi->map_lock) { |
| 578 |
hwirq = bitmap_find_free_region(msi->map, RZG3S_PCI_MSI_INT_NR, |
| 579 |
order_base_2(nr_irqs)); |
| 580 |
} |
| 581 |
|
| 582 |
if (hwirq < 0) |
| 583 |
return -ENOSPC; |
| 584 |
|
| 585 |
for (unsigned int i = 0; i < nr_irqs; i++) { |
| 586 |
irq_domain_set_info(domain, virq + i, hwirq + i, |
| 587 |
&rzg3s_pcie_msi_bottom_chip, |
| 588 |
domain->host_data, handle_edge_irq, NULL, |
| 589 |
NULL); |
| 590 |
} |
| 591 |
|
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
static void rzg3s_pcie_msi_domain_free(struct irq_domain *domain, |
| 596 |
unsigned int virq, unsigned int nr_irqs) |
| 597 |
{ |
| 598 |
struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 599 |
struct rzg3s_pcie_msi *msi = domain->host_data; |
| 600 |
|
| 601 |
guard(mutex)(&msi->map_lock); |
| 602 |
|
| 603 |
bitmap_release_region(msi->map, d->hwirq, order_base_2(nr_irqs)); |
| 604 |
} |
| 605 |
|
| 606 |
static const struct irq_domain_ops rzg3s_pcie_msi_domain_ops = { |
| 607 |
.alloc = rzg3s_pcie_msi_domain_alloc, |
| 608 |
.free = rzg3s_pcie_msi_domain_free, |
| 609 |
}; |
| 610 |
|
| 611 |
#define RZG3S_PCIE_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \ |
| 612 |
MSI_FLAG_USE_DEF_CHIP_OPS | \ |
| 613 |
MSI_FLAG_NO_AFFINITY | \ |
| 614 |
MSI_FLAG_PCI_MSI_MASK_PARENT) |
| 615 |
|
| 616 |
#define RZG3S_PCIE_MSI_FLAGS_SUPPORTED (MSI_FLAG_MULTI_PCI_MSI | \ |
| 617 |
MSI_GENERIC_FLAGS_MASK) |
| 618 |
|
| 619 |
static const struct msi_parent_ops rzg3s_pcie_msi_parent_ops = { |
| 620 |
.required_flags = RZG3S_PCIE_MSI_FLAGS_REQUIRED, |
| 621 |
.supported_flags = RZG3S_PCIE_MSI_FLAGS_SUPPORTED, |
| 622 |
.bus_select_token = DOMAIN_BUS_PCI_MSI, |
| 623 |
.chip_flags = MSI_CHIP_FLAG_SET_ACK, |
| 624 |
.prefix = "RZG3S-", |
| 625 |
.init_dev_msi_info = msi_lib_init_dev_msi_info, |
| 626 |
}; |
| 627 |
|
| 628 |
static int rzg3s_pcie_msi_allocate_domains(struct rzg3s_pcie_msi *msi) |
| 629 |
{ |
| 630 |
struct rzg3s_pcie_host *host = rzg3s_msi_to_host(msi); |
| 631 |
struct device *dev = host->dev; |
| 632 |
struct irq_domain_info info = { |
| 633 |
.fwnode = dev_fwnode(dev), |
| 634 |
.ops = &rzg3s_pcie_msi_domain_ops, |
| 635 |
.size = RZG3S_PCI_MSI_INT_NR, |
| 636 |
.host_data = msi, |
| 637 |
}; |
| 638 |
|
| 639 |
msi->domain = msi_create_parent_irq_domain(&info, |
| 640 |
&rzg3s_pcie_msi_parent_ops); |
| 641 |
if (!msi->domain) |
| 642 |
return dev_err_probe(dev, -ENOMEM, |
| 643 |
"failed to create IRQ domain\n"); |
| 644 |
|
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
static int rzg3s_pcie_msi_hw_setup(struct rzg3s_pcie_host *host) |
| 649 |
{ |
| 650 |
u8 regs = RZG3S_PCI_MSI_INT_NR / RZG3S_PCI_MSI_INT_PER_REG; |
| 651 |
struct rzg3s_pcie_msi *msi = &host->msi; |
| 652 |
|
| 653 |
/* |
| 654 |
* Set MSI window size. HW will set the window to |
| 655 |
* RZG3S_PCI_MSI_INT_NR * 4 bytes. |
| 656 |
*/ |
| 657 |
writel_relaxed(FIELD_PREP(RZG3S_PCI_MSIRCVWMSKL_MASK, |
| 658 |
RZG3S_PCI_MSI_INT_NR - 1), |
| 659 |
host->axi + RZG3S_PCI_MSIRCVWMSKL); |
| 660 |
|
| 661 |
/* Set MSI window address and enable MSI window */ |
| 662 |
writel_relaxed(upper_32_bits(msi->window_base), |
| 663 |
host->axi + RZG3S_PCI_MSIRCVWADRU); |
| 664 |
writel_relaxed(lower_32_bits(msi->window_base) | |
| 665 |
RZG3S_PCI_MSIRCVWADRL_ENA | |
| 666 |
RZG3S_PCI_MSIRCVWADRL_MSG_DATA_ENA, |
| 667 |
host->axi + RZG3S_PCI_MSIRCVWADRL); |
| 668 |
|
| 669 |
/* Set MSI receive enable */ |
| 670 |
for (u8 reg_id = 0; reg_id < regs; reg_id++) { |
| 671 |
writel_relaxed(RZG3S_PCI_MSIRE_ENA, |
| 672 |
host->axi + RZG3S_PCI_MSIRE(reg_id)); |
| 673 |
} |
| 674 |
|
| 675 |
/* Enable message receive interrupts */ |
| 676 |
writel_relaxed(RZG3S_PCI_MSGRCVIE_MSG_RCV, |
| 677 |
host->axi + RZG3S_PCI_MSGRCVIE); |
| 678 |
|
| 679 |
/* Enable MSI */ |
| 680 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PINTRCVIE, |
| 681 |
RZG3S_PCI_PINTRCVIE_MSI, |
| 682 |
RZG3S_PCI_PINTRCVIE_MSI); |
| 683 |
|
| 684 |
return 0; |
| 685 |
} |
| 686 |
|
| 687 |
static int rzg3s_pcie_msi_setup(struct rzg3s_pcie_host *host) |
| 688 |
{ |
| 689 |
size_t size = RZG3S_PCI_MSI_INT_NR * sizeof(u32); |
| 690 |
struct rzg3s_pcie_msi *msi = &host->msi; |
| 691 |
struct device *dev = host->dev; |
| 692 |
int id, ret; |
| 693 |
|
| 694 |
msi->pages = __get_free_pages(GFP_KERNEL | GFP_DMA, 0); |
| 695 |
if (!msi->pages) |
| 696 |
return -ENOMEM; |
| 697 |
|
| 698 |
msi->dma_addr = dma_map_single(dev, (void *)msi->pages, size * 2, |
| 699 |
DMA_BIDIRECTIONAL); |
| 700 |
if (dma_mapping_error(dev, msi->dma_addr)) { |
| 701 |
ret = -ENOMEM; |
| 702 |
goto free_pages; |
| 703 |
} |
| 704 |
|
| 705 |
/* |
| 706 |
* According to the RZ/G3S HW manual (Rev.1.10, section 34.4.5.2 Setting |
| 707 |
* the MSI Window) the MSI window needs to fall within one of the |
| 708 |
* enabled AXI windows. Find an enabled AXI window to setup the MSI |
| 709 |
* window. |
| 710 |
*/ |
| 711 |
for (id = 0; id < RZG3S_MAX_WINDOWS; id++) { |
| 712 |
u64 base, basel, baseu; |
| 713 |
u64 mask, maskl, masku; |
| 714 |
|
| 715 |
basel = readl_relaxed(host->axi + RZG3S_PCI_AWBASEL(id)); |
| 716 |
/* Skip checking this AXI window if it's not enabled */ |
| 717 |
if (!(basel & RZG3S_PCI_AWBASEL_WIN_ENA)) |
| 718 |
continue; |
| 719 |
|
| 720 |
baseu = readl_relaxed(host->axi + RZG3S_PCI_AWBASEU(id)); |
| 721 |
base = baseu << 32 | basel; |
| 722 |
|
| 723 |
maskl = readl_relaxed(host->axi + RZG3S_PCI_AWMASKL(id)); |
| 724 |
masku = readl_relaxed(host->axi + RZG3S_PCI_AWMASKU(id)); |
| 725 |
mask = masku << 32 | maskl; |
| 726 |
|
| 727 |
if (msi->dma_addr < base || msi->dma_addr > base + mask) |
| 728 |
continue; |
| 729 |
|
| 730 |
break; |
| 731 |
} |
| 732 |
|
| 733 |
if (id == RZG3S_MAX_WINDOWS) { |
| 734 |
ret = -EINVAL; |
| 735 |
goto dma_unmap; |
| 736 |
} |
| 737 |
|
| 738 |
/* The MSI base address must be aligned to the MSI size */ |
| 739 |
msi->window_base = ALIGN(msi->dma_addr, size); |
| 740 |
if (msi->window_base < msi->dma_addr) { |
| 741 |
ret = -EINVAL; |
| 742 |
goto dma_unmap; |
| 743 |
} |
| 744 |
|
| 745 |
rzg3s_pcie_msi_hw_setup(host); |
| 746 |
|
| 747 |
return 0; |
| 748 |
|
| 749 |
dma_unmap: |
| 750 |
dma_unmap_single(dev, msi->dma_addr, size * 2, DMA_BIDIRECTIONAL); |
| 751 |
free_pages: |
| 752 |
free_pages(msi->pages, 0); |
| 753 |
return ret; |
| 754 |
} |
| 755 |
|
| 756 |
static void rzg3s_pcie_msi_hw_teardown(struct rzg3s_pcie_host *host) |
| 757 |
{ |
| 758 |
u8 regs = RZG3S_PCI_MSI_INT_NR / RZG3S_PCI_MSI_INT_PER_REG; |
| 759 |
|
| 760 |
/* Disable MSI */ |
| 761 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PINTRCVIE, |
| 762 |
RZG3S_PCI_PINTRCVIE_MSI, 0); |
| 763 |
|
| 764 |
/* Disable message receive interrupts */ |
| 765 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_MSGRCVIE, |
| 766 |
RZG3S_PCI_MSGRCVIE_MSG_RCV, 0); |
| 767 |
|
| 768 |
/* Disable MSI receive enable */ |
| 769 |
for (u8 reg_id = 0; reg_id < regs; reg_id++) |
| 770 |
writel_relaxed(0, host->axi + RZG3S_PCI_MSIRE(reg_id)); |
| 771 |
|
| 772 |
/* Disable MSI window */ |
| 773 |
writel_relaxed(0, host->axi + RZG3S_PCI_MSIRCVWADRL); |
| 774 |
} |
| 775 |
|
| 776 |
static void rzg3s_pcie_teardown_msi(struct rzg3s_pcie_host *host) |
| 777 |
{ |
| 778 |
size_t size = RZG3S_PCI_MSI_INT_NR * sizeof(u32); |
| 779 |
struct rzg3s_pcie_msi *msi = &host->msi; |
| 780 |
|
| 781 |
rzg3s_pcie_msi_hw_teardown(host); |
| 782 |
|
| 783 |
free_irq(msi->irq, host); |
| 784 |
irq_domain_remove(msi->domain); |
| 785 |
|
| 786 |
/* Free unused memory */ |
| 787 |
dma_unmap_single(host->dev, msi->dma_addr, size * 2, DMA_BIDIRECTIONAL); |
| 788 |
free_pages(msi->pages, 0); |
| 789 |
} |
| 790 |
|
| 791 |
static int rzg3s_pcie_init_msi(struct rzg3s_pcie_host *host) |
| 792 |
{ |
| 793 |
struct platform_device *pdev = to_platform_device(host->dev); |
| 794 |
struct rzg3s_pcie_msi *msi = &host->msi; |
| 795 |
struct device *dev = host->dev; |
| 796 |
const char *devname; |
| 797 |
int ret; |
| 798 |
|
| 799 |
ret = devm_mutex_init(dev, &msi->map_lock); |
| 800 |
if (ret) |
| 801 |
return ret; |
| 802 |
|
| 803 |
msi->irq = platform_get_irq_byname(pdev, "msi"); |
| 804 |
if (msi->irq < 0) |
| 805 |
return dev_err_probe(dev, msi->irq, "Failed to get MSI IRQ!\n"); |
| 806 |
|
| 807 |
devname = devm_kasprintf(dev, GFP_KERNEL, "%s-msi", dev_name(dev)); |
| 808 |
if (!devname) |
| 809 |
return -ENOMEM; |
| 810 |
|
| 811 |
ret = rzg3s_pcie_msi_allocate_domains(msi); |
| 812 |
if (ret) |
| 813 |
return ret; |
| 814 |
|
| 815 |
/* |
| 816 |
* Don't use devm_request_irq() as the driver uses non-devm helpers |
| 817 |
* to control clocks. Mixing them may lead to subtle bugs. |
| 818 |
*/ |
| 819 |
ret = request_irq(msi->irq, rzg3s_pcie_msi_irq, 0, devname, host); |
| 820 |
if (ret) { |
| 821 |
dev_err_probe(dev, ret, "Failed to request IRQ: %d\n", ret); |
| 822 |
goto free_domains; |
| 823 |
} |
| 824 |
|
| 825 |
ret = rzg3s_pcie_msi_setup(host); |
| 826 |
if (ret) { |
| 827 |
dev_err_probe(dev, ret, "Failed to setup MSI!\n"); |
| 828 |
goto free_irq; |
| 829 |
} |
| 830 |
|
| 831 |
return 0; |
| 832 |
|
| 833 |
free_irq: |
| 834 |
free_irq(msi->irq, host); |
| 835 |
free_domains: |
| 836 |
irq_domain_remove(msi->domain); |
| 837 |
return ret; |
| 838 |
} |
| 839 |
|
| 840 |
static void rzg3s_pcie_intx_irq_ack(struct irq_data *d) |
| 841 |
{ |
| 842 |
struct rzg3s_pcie_host *host = irq_data_get_irq_chip_data(d); |
| 843 |
|
| 844 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PINTRCVIS, |
| 845 |
RZG3S_PCI_PINTRCVIS_INTX(d->hwirq), |
| 846 |
RZG3S_PCI_PINTRCVIS_INTX(d->hwirq)); |
| 847 |
} |
| 848 |
|
| 849 |
static void rzg3s_pcie_intx_irq_mask(struct irq_data *d) |
| 850 |
{ |
| 851 |
struct rzg3s_pcie_host *host = irq_data_get_irq_chip_data(d); |
| 852 |
|
| 853 |
guard(raw_spinlock_irqsave)(&host->hw_lock); |
| 854 |
|
| 855 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PINTRCVIE, |
| 856 |
RZG3S_PCI_PINTRCVIE_INTX(d->hwirq), 0); |
| 857 |
} |
| 858 |
|
| 859 |
static void rzg3s_pcie_intx_irq_unmask(struct irq_data *d) |
| 860 |
{ |
| 861 |
struct rzg3s_pcie_host *host = irq_data_get_irq_chip_data(d); |
| 862 |
|
| 863 |
guard(raw_spinlock_irqsave)(&host->hw_lock); |
| 864 |
|
| 865 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PINTRCVIE, |
| 866 |
RZG3S_PCI_PINTRCVIE_INTX(d->hwirq), |
| 867 |
RZG3S_PCI_PINTRCVIE_INTX(d->hwirq)); |
| 868 |
} |
| 869 |
|
| 870 |
static struct irq_chip rzg3s_pcie_intx_irq_chip = { |
| 871 |
.name = "PCIe INTx", |
| 872 |
.irq_ack = rzg3s_pcie_intx_irq_ack, |
| 873 |
.irq_mask = rzg3s_pcie_intx_irq_mask, |
| 874 |
.irq_unmask = rzg3s_pcie_intx_irq_unmask, |
| 875 |
}; |
| 876 |
|
| 877 |
static int rzg3s_pcie_intx_map(struct irq_domain *domain, unsigned int irq, |
| 878 |
irq_hw_number_t hwirq) |
| 879 |
{ |
| 880 |
irq_set_chip_and_handler(irq, &rzg3s_pcie_intx_irq_chip, |
| 881 |
handle_level_irq); |
| 882 |
irq_set_chip_data(irq, domain->host_data); |
| 883 |
|
| 884 |
return 0; |
| 885 |
} |
| 886 |
|
| 887 |
static const struct irq_domain_ops rzg3s_pcie_intx_domain_ops = { |
| 888 |
.map = rzg3s_pcie_intx_map, |
| 889 |
.xlate = irq_domain_xlate_onetwocell, |
| 890 |
}; |
| 891 |
|
| 892 |
static int rzg3s_pcie_init_irqdomain(struct rzg3s_pcie_host *host) |
| 893 |
{ |
| 894 |
struct device *dev = host->dev; |
| 895 |
struct platform_device *pdev = to_platform_device(dev); |
| 896 |
|
| 897 |
for (int i = 0; i < PCI_NUM_INTX; i++) { |
| 898 |
char irq_name[5] = {0}; |
| 899 |
int irq; |
| 900 |
|
| 901 |
scnprintf(irq_name, ARRAY_SIZE(irq_name), "int%c", 'a' + i); |
| 902 |
|
| 903 |
irq = platform_get_irq_byname(pdev, irq_name); |
| 904 |
if (irq < 0) |
| 905 |
return dev_err_probe(dev, -EINVAL, |
| 906 |
"Failed to parse and map INT%c IRQ\n", |
| 907 |
'A' + i); |
| 908 |
|
| 909 |
host->intx_irqs[i] = irq; |
| 910 |
irq_set_chained_handler_and_data(irq, |
| 911 |
rzg3s_pcie_intx_irq_handler, |
| 912 |
host); |
| 913 |
} |
| 914 |
|
| 915 |
host->intx_domain = irq_domain_create_linear(dev_fwnode(dev), |
| 916 |
PCI_NUM_INTX, |
| 917 |
&rzg3s_pcie_intx_domain_ops, |
| 918 |
host); |
| 919 |
if (!host->intx_domain) |
| 920 |
return dev_err_probe(dev, -EINVAL, |
| 921 |
"Failed to add irq domain for INTx IRQs\n"); |
| 922 |
irq_domain_update_bus_token(host->intx_domain, DOMAIN_BUS_WIRED); |
| 923 |
|
| 924 |
if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 925 |
int ret = rzg3s_pcie_init_msi(host); |
| 926 |
|
| 927 |
if (ret) { |
| 928 |
irq_domain_remove(host->intx_domain); |
| 929 |
return ret; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
return 0; |
| 934 |
} |
| 935 |
|
| 936 |
static void rzg3s_pcie_teardown_irqdomain(struct rzg3s_pcie_host *host) |
| 937 |
{ |
| 938 |
if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 939 |
rzg3s_pcie_teardown_msi(host); |
| 940 |
|
| 941 |
irq_domain_remove(host->intx_domain); |
| 942 |
} |
| 943 |
|
| 944 |
static int rzg3s_pcie_set_max_link_speed(struct rzg3s_pcie_host *host) |
| 945 |
{ |
| 946 |
u32 remote_supported_link_speeds, max_supported_link_speeds; |
| 947 |
u32 cs2, tmp, pcie_cap = RZG3S_PCI_CFG_PCIEC; |
| 948 |
u32 cur_link_speed, link_speed; |
| 949 |
u8 ltssm_state_l0 = 0xc; |
| 950 |
int ret; |
| 951 |
u16 ls; |
| 952 |
|
| 953 |
/* |
| 954 |
* According to the RZ/G3S HW manual (Rev.1.10, section 34.6.3 Caution |
| 955 |
* when Changing the Speed Spontaneously) link speed change can be done |
| 956 |
* only when the LTSSM is in L0. |
| 957 |
*/ |
| 958 |
ret = readl_poll_timeout(host->axi + RZG3S_PCI_PCSTAT1, tmp, |
| 959 |
FIELD_GET(RZG3S_PCI_PCSTAT1_LTSSM_STATE, tmp) == ltssm_state_l0, |
| 960 |
PCIE_LINK_WAIT_SLEEP_MS * MILLI, |
| 961 |
PCIE_LINK_WAIT_SLEEP_MS * MILLI * |
| 962 |
PCIE_LINK_WAIT_MAX_RETRIES); |
| 963 |
if (ret) |
| 964 |
return ret; |
| 965 |
|
| 966 |
ls = readw_relaxed(host->pcie + pcie_cap + PCI_EXP_LNKSTA); |
| 967 |
cs2 = readl_relaxed(host->axi + RZG3S_PCI_PCSTAT2); |
| 968 |
|
| 969 |
switch (pcie_link_speed[host->max_link_speed]) { |
| 970 |
case PCIE_SPEED_5_0GT: |
| 971 |
max_supported_link_speeds = GENMASK(PCI_EXP_LNKSTA_CLS_5_0GB - 1, 0); |
| 972 |
link_speed = PCI_EXP_LNKCTL2_TLS_5_0GT; |
| 973 |
break; |
| 974 |
default: |
| 975 |
/* Should not happen */ |
| 976 |
return -EINVAL; |
| 977 |
} |
| 978 |
|
| 979 |
cur_link_speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, ls); |
| 980 |
remote_supported_link_speeds = FIELD_GET(RZG3S_PCI_PCSTAT2_SDRIRE, cs2); |
| 981 |
/* Drop reserved bits */ |
| 982 |
remote_supported_link_speeds &= max_supported_link_speeds; |
| 983 |
|
| 984 |
/* |
| 985 |
* Return if max link speed is already set or the connected device |
| 986 |
* doesn't support it. |
| 987 |
*/ |
| 988 |
if (cur_link_speed == host->max_link_speed || |
| 989 |
remote_supported_link_speeds != max_supported_link_speeds) |
| 990 |
return 0; |
| 991 |
|
| 992 |
/* Set target Link speed */ |
| 993 |
rzg3s_pcie_update_bits(host->pcie, pcie_cap + PCI_EXP_LNKCTL2, |
| 994 |
PCI_EXP_LNKCTL2_TLS, |
| 995 |
FIELD_PREP(PCI_EXP_LNKCTL2_TLS, link_speed)); |
| 996 |
|
| 997 |
/* Request link speed change */ |
| 998 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PCCTRL2, |
| 999 |
RZG3S_PCI_PCCTRL2_LS_CHG_REQ | |
| 1000 |
RZG3S_PCI_PCCTRL2_LS_CHG, |
| 1001 |
RZG3S_PCI_PCCTRL2_LS_CHG_REQ | |
| 1002 |
FIELD_PREP(RZG3S_PCI_PCCTRL2_LS_CHG, |
| 1003 |
link_speed - 1)); |
| 1004 |
|
| 1005 |
ret = readl_poll_timeout(host->axi + RZG3S_PCI_PCSTAT2, cs2, |
| 1006 |
(cs2 & RZG3S_PCI_PCSTAT2_LS_CHG_DONE), |
| 1007 |
PCIE_LINK_WAIT_SLEEP_MS * MILLI, |
| 1008 |
PCIE_LINK_WAIT_SLEEP_MS * MILLI * |
| 1009 |
PCIE_LINK_WAIT_MAX_RETRIES); |
| 1010 |
|
| 1011 |
/* |
| 1012 |
* According to the RZ/G3S HW manual (Rev.1.10, section 34.6.3 Caution |
| 1013 |
* when Changing the Speed Spontaneously) the PCI_PCCTRL2_LS_CHG_REQ |
| 1014 |
* should be de-asserted after checking for PCI_PCSTAT2_LS_CHG_DONE. |
| 1015 |
*/ |
| 1016 |
rzg3s_pcie_update_bits(host->axi, RZG3S_PCI_PCCTRL2, |
| 1017 |
RZG3S_PCI_PCCTRL2_LS_CHG_REQ, 0); |
| 1018 |
|
| 1019 |
return ret; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static int rzg3s_pcie_config_init(struct rzg3s_pcie_host *host) |
| 1023 |
{ |
| 1024 |
struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host); |
| 1025 |
struct resource_entry *ft; |
| 1026 |
struct resource *bus; |
| 1027 |
u8 subordinate_bus; |
| 1028 |
u8 secondary_bus; |
| 1029 |
u8 primary_bus; |
| 1030 |
|
| 1031 |
ft = resource_list_first_type(&bridge->windows, IORESOURCE_BUS); |
| 1032 |
if (!ft) |
| 1033 |
return -ENODEV; |
| 1034 |
|
| 1035 |
bus = ft->res; |
| 1036 |
primary_bus = bus->start; |
| 1037 |
secondary_bus = bus->start + 1; |
| 1038 |
subordinate_bus = bus->end; |
| 1039 |
|
| 1040 |
/* Enable access control to the CFGU */ |
| 1041 |
writel_relaxed(RZG3S_PCI_PERM_CFG_HWINIT_EN, |
| 1042 |
host->axi + RZG3S_PCI_PERM); |
| 1043 |
|
| 1044 |
/* HW manual recommends to write 0xffffffff on initialization */ |
| 1045 |
writel_relaxed(0xffffffff, host->pcie + RZG3S_PCI_CFG_BARMSK00L); |
| 1046 |
writel_relaxed(0xffffffff, host->pcie + RZG3S_PCI_CFG_BARMSK00U); |
| 1047 |
|
| 1048 |
/* Disable access control to the CFGU */ |
| 1049 |
writel_relaxed(0, host->axi + RZG3S_PCI_PERM); |
| 1050 |
|
| 1051 |
/* Update bus info */ |
| 1052 |
writeb_relaxed(primary_bus, host->pcie + PCI_PRIMARY_BUS); |
| 1053 |
writeb_relaxed(secondary_bus, host->pcie + PCI_SECONDARY_BUS); |
| 1054 |
writeb_relaxed(subordinate_bus, host->pcie + PCI_SUBORDINATE_BUS); |
| 1055 |
|
| 1056 |
return 0; |
| 1057 |
} |
| 1058 |
|
| 1059 |
static void rzg3s_pcie_irq_init(struct rzg3s_pcie_host *host) |
| 1060 |
{ |
| 1061 |
/* |
| 1062 |
* According to the HW manual of the RZ/G3S (Rev.1.10, sections |
| 1063 |
* corresponding to all registers written with ~0U), the hardware |
| 1064 |
* ignores value written to unused bits. Writing ~0U to these registers |
| 1065 |
* should be safe. |
| 1066 |
*/ |
| 1067 |
|
| 1068 |
/* Clear the link state and PM transitions */ |
| 1069 |
writel_relaxed(RZG3S_PCI_PEIS0_DL_UPDOWN | |
| 1070 |
RZG3S_PCI_PEIS0_RX_DLLP_PM_ENTER, |
| 1071 |
host->axi + RZG3S_PCI_PEIS0); |
| 1072 |
|
| 1073 |
/* Disable all interrupts */ |
| 1074 |
writel_relaxed(0, host->axi + RZG3S_PCI_PEIE0); |
| 1075 |
|
| 1076 |
/* Clear all parity and ecc error interrupts */ |
| 1077 |
writel_relaxed(~0U, host->axi + RZG3S_PCI_PEIS1); |
| 1078 |
|
| 1079 |
/* Disable all parity and ecc error interrupts */ |
| 1080 |
writel_relaxed(0, host->axi + RZG3S_PCI_PEIE1); |
| 1081 |
|
| 1082 |
/* Clear all AXI master error interrupts */ |
| 1083 |
writel_relaxed(~0U, host->axi + RZG3S_PCI_AMEIS); |
| 1084 |
|
| 1085 |
/* Clear all AXI slave error interrupts */ |
| 1086 |
writel_relaxed(~0U, host->axi + RZG3S_PCI_ASEIS1); |
| 1087 |
|
| 1088 |
/* Clear all message receive interrupts */ |
| 1089 |
writel_relaxed(~0U, host->axi + RZG3S_PCI_MSGRCVIS); |
| 1090 |
} |
| 1091 |
|
| 1092 |
static int rzg3s_pcie_power_resets_deassert(struct rzg3s_pcie_host *host) |
| 1093 |
{ |
| 1094 |
const struct rzg3s_pcie_soc_data *data = host->data; |
| 1095 |
|
| 1096 |
/* |
| 1097 |
* According to the RZ/G3S HW manual (Rev.1.10, section |
| 1098 |
* 34.5.1.2 De-asserting the Reset) the PCIe IP needs to wait 5ms from |
| 1099 |
* power on to the de-assertion of reset. |
| 1100 |
*/ |
| 1101 |
fsleep(5000); |
| 1102 |
return reset_control_bulk_deassert(data->num_power_resets, |
| 1103 |
host->power_resets); |
| 1104 |
} |
| 1105 |
|
| 1106 |
static int rzg3s_pcie_resets_prepare_and_get(struct rzg3s_pcie_host *host) |
| 1107 |
{ |
| 1108 |
const struct rzg3s_pcie_soc_data *data = host->data; |
| 1109 |
unsigned int i; |
| 1110 |
int ret; |
| 1111 |
|
| 1112 |
host->power_resets = devm_kmalloc_array(host->dev, |
| 1113 |
data->num_power_resets, |
| 1114 |
sizeof(*host->power_resets), |
| 1115 |
GFP_KERNEL); |
| 1116 |
if (!host->power_resets) |
| 1117 |
return -ENOMEM; |
| 1118 |
|
| 1119 |
for (i = 0; i < data->num_power_resets; i++) |
| 1120 |
host->power_resets[i].id = data->power_resets[i]; |
| 1121 |
|
| 1122 |
host->cfg_resets = devm_kmalloc_array(host->dev, |
| 1123 |
data->num_cfg_resets, |
| 1124 |
sizeof(*host->cfg_resets), |
| 1125 |
GFP_KERNEL); |
| 1126 |
if (!host->cfg_resets) |
| 1127 |
return -ENOMEM; |
| 1128 |
|
| 1129 |
for (i = 0; i < data->num_cfg_resets; i++) |
| 1130 |
host->cfg_resets[i].id = data->cfg_resets[i]; |
| 1131 |
|
| 1132 |
ret = devm_reset_control_bulk_get_exclusive(host->dev, |
| 1133 |
data->num_power_resets, |
| 1134 |
host->power_resets); |
| 1135 |
if (ret) |
| 1136 |
return ret; |
| 1137 |
|
| 1138 |
return devm_reset_control_bulk_get_exclusive(host->dev, |
| 1139 |
data->num_cfg_resets, |
| 1140 |
host->cfg_resets); |
| 1141 |
} |
| 1142 |
|
| 1143 |
static int rzg3s_pcie_host_parse_port(struct rzg3s_pcie_host *host) |
| 1144 |
{ |
| 1145 |
struct device_node *of_port __free(device_node) = |
| 1146 |
of_get_next_child(host->dev->of_node, NULL); |
| 1147 |
struct rzg3s_pcie_port *port = &host->port; |
| 1148 |
int ret; |
| 1149 |
|
| 1150 |
ret = of_property_read_u32(of_port, "vendor-id", &port->vendor_id); |
| 1151 |
if (ret) |
| 1152 |
return ret; |
| 1153 |
|
| 1154 |
ret = of_property_read_u32(of_port, "device-id", &port->device_id); |
| 1155 |
if (ret) |
| 1156 |
return ret; |
| 1157 |
|
| 1158 |
port->refclk = of_clk_get_by_name(of_port, "ref"); |
| 1159 |
if (IS_ERR(port->refclk)) |
| 1160 |
return PTR_ERR(port->refclk); |
| 1161 |
|
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static int rzg3s_pcie_host_init_port(struct rzg3s_pcie_host *host) |
| 1166 |
{ |
| 1167 |
struct rzg3s_pcie_port *port = &host->port; |
| 1168 |
struct device *dev = host->dev; |
| 1169 |
int ret; |
| 1170 |
|
| 1171 |
/* Enable access control to the CFGU */ |
| 1172 |
writel_relaxed(RZG3S_PCI_PERM_CFG_HWINIT_EN, |
| 1173 |
host->axi + RZG3S_PCI_PERM); |
| 1174 |
|
| 1175 |
/* Update vendor ID and device ID */ |
| 1176 |
writew_relaxed(port->vendor_id, host->pcie + PCI_VENDOR_ID); |
| 1177 |
writew_relaxed(port->device_id, host->pcie + PCI_DEVICE_ID); |
| 1178 |
|
| 1179 |
/* Disable access control to the CFGU */ |
| 1180 |
writel_relaxed(0, host->axi + RZG3S_PCI_PERM); |
| 1181 |
|
| 1182 |
ret = clk_prepare_enable(port->refclk); |
| 1183 |
if (ret) |
| 1184 |
return dev_err_probe(dev, ret, "Failed to enable refclk!\n"); |
| 1185 |
|
| 1186 |
/* Set the PHY, if any */ |
| 1187 |
if (host->data->init_phy) { |
| 1188 |
ret = host->data->init_phy(host); |
| 1189 |
if (ret) { |
| 1190 |
dev_err_probe(dev, ret, "Failed to set the PHY!\n"); |
| 1191 |
goto refclk_disable; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
return 0; |
| 1196 |
|
| 1197 |
refclk_disable: |
| 1198 |
clk_disable_unprepare(port->refclk); |
| 1199 |
return ret; |
| 1200 |
} |
| 1201 |
|
| 1202 |
static int rzg3s_pcie_host_init(struct rzg3s_pcie_host *host) |
| 1203 |
{ |
| 1204 |
u32 val; |
| 1205 |
int ret; |
| 1206 |
|
| 1207 |
/* Initialize the PCIe related registers */ |
| 1208 |
ret = rzg3s_pcie_config_init(host); |
| 1209 |
if (ret) |
| 1210 |
return ret; |
| 1211 |
|
| 1212 |
ret = rzg3s_pcie_host_init_port(host); |
| 1213 |
if (ret) |
| 1214 |
return ret; |
| 1215 |
|
| 1216 |
/* Initialize the interrupts */ |
| 1217 |
rzg3s_pcie_irq_init(host); |
| 1218 |
|
| 1219 |
ret = reset_control_bulk_deassert(host->data->num_cfg_resets, |
| 1220 |
host->cfg_resets); |
| 1221 |
if (ret) |
| 1222 |
goto disable_port_refclk; |
| 1223 |
|
| 1224 |
/* Wait for link up */ |
| 1225 |
ret = readl_poll_timeout(host->axi + RZG3S_PCI_PCSTAT1, val, |
| 1226 |
!(val & RZG3S_PCI_PCSTAT1_DL_DOWN_STS), |
| 1227 |
PCIE_LINK_WAIT_SLEEP_MS * MILLI, |
| 1228 |
PCIE_LINK_WAIT_SLEEP_MS * MILLI * |
| 1229 |
PCIE_LINK_WAIT_MAX_RETRIES); |
| 1230 |
if (ret) |
| 1231 |
goto cfg_resets_deassert; |
| 1232 |
|
| 1233 |
val = readl_relaxed(host->axi + RZG3S_PCI_PCSTAT2); |
| 1234 |
dev_info(host->dev, "PCIe link status [0x%x]\n", val); |
| 1235 |
|
| 1236 |
return 0; |
| 1237 |
|
| 1238 |
cfg_resets_deassert: |
| 1239 |
reset_control_bulk_assert(host->data->num_cfg_resets, |
| 1240 |
host->cfg_resets); |
| 1241 |
disable_port_refclk: |
| 1242 |
clk_disable_unprepare(host->port.refclk); |
| 1243 |
return ret; |
| 1244 |
} |
| 1245 |
|
| 1246 |
static void rzg3s_pcie_set_inbound_window(struct rzg3s_pcie_host *host, |
| 1247 |
u64 cpu_addr, u64 pci_addr, u64 size, |
| 1248 |
int id) |
| 1249 |
{ |
| 1250 |
/* Set CPU window base address */ |
| 1251 |
writel_relaxed(upper_32_bits(cpu_addr), |
| 1252 |
host->axi + RZG3S_PCI_ADESTU(id)); |
| 1253 |
writel_relaxed(lower_32_bits(cpu_addr), |
| 1254 |
host->axi + RZG3S_PCI_ADESTL(id)); |
| 1255 |
|
| 1256 |
/* Set window size */ |
| 1257 |
writel_relaxed(upper_32_bits(size), host->axi + RZG3S_PCI_AWMASKU(id)); |
| 1258 |
writel_relaxed(lower_32_bits(size), host->axi + RZG3S_PCI_AWMASKL(id)); |
| 1259 |
|
| 1260 |
/* Set PCIe window base address and enable the window */ |
| 1261 |
writel_relaxed(upper_32_bits(pci_addr), |
| 1262 |
host->axi + RZG3S_PCI_AWBASEU(id)); |
| 1263 |
writel_relaxed(lower_32_bits(pci_addr) | RZG3S_PCI_AWBASEL_WIN_ENA, |
| 1264 |
host->axi + RZG3S_PCI_AWBASEL(id)); |
| 1265 |
} |
| 1266 |
|
| 1267 |
static int rzg3s_pcie_set_inbound_windows(struct rzg3s_pcie_host *host, |
| 1268 |
struct resource_entry *entry, |
| 1269 |
int *index) |
| 1270 |
{ |
| 1271 |
u64 pci_addr = entry->res->start - entry->offset; |
| 1272 |
u64 cpu_addr = entry->res->start; |
| 1273 |
u64 cpu_end = entry->res->end; |
| 1274 |
u64 size_id = 0; |
| 1275 |
int id = *index; |
| 1276 |
u64 size; |
| 1277 |
|
| 1278 |
while (cpu_addr < cpu_end) { |
| 1279 |
if (id >= RZG3S_MAX_WINDOWS) |
| 1280 |
return dev_err_probe(host->dev, -ENOSPC, |
| 1281 |
"Failed to map inbound window for resource (%s)\n", |
| 1282 |
entry->res->name); |
| 1283 |
|
| 1284 |
size = resource_size(entry->res) - size_id; |
| 1285 |
|
| 1286 |
/* |
| 1287 |
* According to the RZ/G3S HW manual (Rev.1.10, |
| 1288 |
* section 34.3.1.71 AXI Window Mask (Lower) Registers) the min |
| 1289 |
* size is 4K. |
| 1290 |
*/ |
| 1291 |
size = max(size, SZ_4K); |
| 1292 |
|
| 1293 |
/* |
| 1294 |
* According the RZ/G3S HW manual (Rev.1.10, sections: |
| 1295 |
* - 34.3.1.69 AXI Window Base (Lower) Registers |
| 1296 |
* - 34.3.1.71 AXI Window Mask (Lower) Registers |
| 1297 |
* - 34.3.1.73 AXI Destination (Lower) Registers) |
| 1298 |
* the CPU addr, PCIe addr, size should be 4K aligned and be a |
| 1299 |
* power of 2. |
| 1300 |
*/ |
| 1301 |
size = ALIGN(size, SZ_4K); |
| 1302 |
size = roundup_pow_of_two(size); |
| 1303 |
|
| 1304 |
cpu_addr = ALIGN(cpu_addr, SZ_4K); |
| 1305 |
pci_addr = ALIGN(pci_addr, SZ_4K); |
| 1306 |
|
| 1307 |
/* |
| 1308 |
* According to the RZ/G3S HW manual (Rev.1.10, section |
| 1309 |
* 34.3.1.71 AXI Window Mask (Lower) Registers) HW expects first |
| 1310 |
* 12 LSB bits to be 0xfff. Subtract 1 from size for this. |
| 1311 |
*/ |
| 1312 |
rzg3s_pcie_set_inbound_window(host, cpu_addr, pci_addr, |
| 1313 |
size - 1, id); |
| 1314 |
|
| 1315 |
pci_addr += size; |
| 1316 |
cpu_addr += size; |
| 1317 |
size_id = size; |
| 1318 |
id++; |
| 1319 |
} |
| 1320 |
*index = id; |
| 1321 |
|
| 1322 |
return 0; |
| 1323 |
} |
| 1324 |
|
| 1325 |
static int rzg3s_pcie_parse_map_dma_ranges(struct rzg3s_pcie_host *host) |
| 1326 |
{ |
| 1327 |
struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host); |
| 1328 |
struct resource_entry *entry; |
| 1329 |
int i = 0, ret; |
| 1330 |
|
| 1331 |
resource_list_for_each_entry(entry, &bridge->dma_ranges) { |
| 1332 |
ret = rzg3s_pcie_set_inbound_windows(host, entry, &i); |
| 1333 |
if (ret) |
| 1334 |
return ret; |
| 1335 |
} |
| 1336 |
|
| 1337 |
return 0; |
| 1338 |
} |
| 1339 |
|
| 1340 |
static void rzg3s_pcie_set_outbound_window(struct rzg3s_pcie_host *host, |
| 1341 |
struct resource_entry *win, int id) |
| 1342 |
{ |
| 1343 |
struct resource *res = win->res; |
| 1344 |
resource_size_t size = resource_size(res); |
| 1345 |
resource_size_t res_start; |
| 1346 |
|
| 1347 |
if (res->flags & IORESOURCE_IO) |
| 1348 |
res_start = pci_pio_to_address(res->start) - win->offset; |
| 1349 |
else |
| 1350 |
res_start = res->start - win->offset; |
| 1351 |
|
| 1352 |
/* |
| 1353 |
* According to the RZ/G3S HW manual (Rev.1.10, section 34.3.1.75 PCIe |
| 1354 |
* Window Base (Lower) Registers) the window base address need to be 4K |
| 1355 |
* aligned. |
| 1356 |
*/ |
| 1357 |
res_start = ALIGN(res_start, SZ_4K); |
| 1358 |
|
| 1359 |
size = ALIGN(size, SZ_4K); |
| 1360 |
size = roundup_pow_of_two(size) - 1; |
| 1361 |
|
| 1362 |
/* Set PCIe destination */ |
| 1363 |
writel_relaxed(upper_32_bits(res_start), |
| 1364 |
host->axi + RZG3S_PCI_PDESTU(id)); |
| 1365 |
writel_relaxed(lower_32_bits(res_start), |
| 1366 |
host->axi + RZG3S_PCI_PDESTL(id)); |
| 1367 |
|
| 1368 |
/* Set PCIe window mask */ |
| 1369 |
writel_relaxed(upper_32_bits(size), host->axi + RZG3S_PCI_PWMASKU(id)); |
| 1370 |
writel_relaxed(lower_32_bits(size), host->axi + RZG3S_PCI_PWMASKL(id)); |
| 1371 |
|
| 1372 |
/* Set PCIe window base and enable the window */ |
| 1373 |
writel_relaxed(upper_32_bits(res_start), |
| 1374 |
host->axi + RZG3S_PCI_PWBASEU(id)); |
| 1375 |
writel_relaxed(lower_32_bits(res_start) | RZG3S_PCI_PWBASEL_ENA, |
| 1376 |
host->axi + RZG3S_PCI_PWBASEL(id)); |
| 1377 |
} |
| 1378 |
|
| 1379 |
static int rzg3s_pcie_parse_map_ranges(struct rzg3s_pcie_host *host) |
| 1380 |
{ |
| 1381 |
struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host); |
| 1382 |
struct resource_entry *win; |
| 1383 |
int i = 0; |
| 1384 |
|
| 1385 |
resource_list_for_each_entry(win, &bridge->windows) { |
| 1386 |
struct resource *res = win->res; |
| 1387 |
|
| 1388 |
if (i >= RZG3S_MAX_WINDOWS) |
| 1389 |
return dev_err_probe(host->dev, -ENOSPC, |
| 1390 |
"Failed to map outbound window for resource (%s)\n", |
| 1391 |
res->name); |
| 1392 |
|
| 1393 |
if (!res->flags) |
| 1394 |
continue; |
| 1395 |
|
| 1396 |
switch (resource_type(res)) { |
| 1397 |
case IORESOURCE_IO: |
| 1398 |
case IORESOURCE_MEM: |
| 1399 |
rzg3s_pcie_set_outbound_window(host, win, i); |
| 1400 |
i++; |
| 1401 |
break; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
return 0; |
| 1406 |
} |
| 1407 |
|
| 1408 |
static int rzg3s_soc_pcie_init_phy(struct rzg3s_pcie_host *host) |
| 1409 |
{ |
| 1410 |
static const u32 xcfgd_settings[RZG3S_PCI_PHY_XCFGD_NUM] = { |
| 1411 |
[8] = 0xe0006801, 0x007f7e30, 0x183e0000, 0x978ff500, |
| 1412 |
0xec000000, 0x009f1400, 0x0000d009, |
| 1413 |
[17] = 0x78000000, |
| 1414 |
[19] = 0x00880000, 0x000005c0, 0x07000000, 0x00780920, |
| 1415 |
0xc9400ce2, 0x90000c0c, 0x000c1414, 0x00005034, |
| 1416 |
0x00006000, 0x00000001, |
| 1417 |
}; |
| 1418 |
static const u32 xcfga_cmn_settings[RZG3S_PCI_PHY_XCFGA_CMN_NUM] = { |
| 1419 |
0x00000d10, 0x08310100, 0x00c21404, 0x013c0010, 0x01874440, |
| 1420 |
0x1a216082, 0x00103440, 0x00000080, 0x00000010, 0x0c1000c1, |
| 1421 |
0x1000c100, 0x0222000c, 0x00640019, 0x00a00028, 0x01d11228, |
| 1422 |
0x0201001d, |
| 1423 |
}; |
| 1424 |
static const u32 xcfga_rx_settings[RZG3S_PCI_PHY_XCFGA_RX_NUM] = { |
| 1425 |
0x07d55000, 0x030e3f00, 0x00000288, 0x102c5880, 0x0000000b, |
| 1426 |
0x04141441, 0x00641641, 0x00d63d63, 0x00641641, 0x01970377, |
| 1427 |
0x00190287, 0x00190028, 0x00000028, |
| 1428 |
}; |
| 1429 |
unsigned int i; |
| 1430 |
|
| 1431 |
/* |
| 1432 |
* Enable access permission for physical layer control and status |
| 1433 |
* registers. |
| 1434 |
*/ |
| 1435 |
writel_relaxed(RZG3S_PCI_PERM_PIPE_PHY_REG_EN, |
| 1436 |
host->axi + RZG3S_PCI_PERM); |
| 1437 |
|
| 1438 |
for (i = 0; i < RZG3S_PCI_PHY_XCFGD_NUM; i++) { |
| 1439 |
writel_relaxed(xcfgd_settings[i], |
| 1440 |
host->axi + RZG3S_PCI_PHY_XCFGD(i)); |
| 1441 |
} |
| 1442 |
|
| 1443 |
for (i = 0; i < RZG3S_PCI_PHY_XCFGA_CMN_NUM; i++) { |
| 1444 |
writel_relaxed(xcfga_cmn_settings[i], |
| 1445 |
host->axi + RZG3S_PCI_PHY_XCFGA_CMN(i)); |
| 1446 |
} |
| 1447 |
|
| 1448 |
for (i = 0; i < RZG3S_PCI_PHY_XCFGA_RX_NUM; i++) { |
| 1449 |
writel_relaxed(xcfga_rx_settings[i], |
| 1450 |
host->axi + RZG3S_PCI_PHY_XCFGA_RX(i)); |
| 1451 |
} |
| 1452 |
|
| 1453 |
writel_relaxed(0x107, host->axi + RZG3S_PCI_PHY_XCFGA_TX); |
| 1454 |
|
| 1455 |
/* Select PHY settings values */ |
| 1456 |
writel_relaxed(RZG3S_PCI_PHY_XCFG_CTRL_PHYREG_SEL, |
| 1457 |
host->axi + RZG3S_PCI_PHY_XCFG_CTRL); |
| 1458 |
|
| 1459 |
/* |
| 1460 |
* Disable access permission for physical layer control and status |
| 1461 |
* registers. |
| 1462 |
*/ |
| 1463 |
writel_relaxed(0, host->axi + RZG3S_PCI_PERM); |
| 1464 |
|
| 1465 |
return 0; |
| 1466 |
} |
| 1467 |
|
| 1468 |
static int |
| 1469 |
rzg3s_pcie_host_setup(struct rzg3s_pcie_host *host, |
| 1470 |
int (*init_irqdomain)(struct rzg3s_pcie_host *host), |
| 1471 |
void (*teardown_irqdomain)(struct rzg3s_pcie_host *host)) |
| 1472 |
{ |
| 1473 |
struct device *dev = host->dev; |
| 1474 |
int ret; |
| 1475 |
|
| 1476 |
/* Set inbound windows */ |
| 1477 |
ret = rzg3s_pcie_parse_map_dma_ranges(host); |
| 1478 |
if (ret) |
| 1479 |
return dev_err_probe(dev, ret, |
| 1480 |
"Failed to set inbound windows!\n"); |
| 1481 |
|
| 1482 |
/* Set outbound windows */ |
| 1483 |
ret = rzg3s_pcie_parse_map_ranges(host); |
| 1484 |
if (ret) |
| 1485 |
return dev_err_probe(dev, ret, |
| 1486 |
"Failed to set outbound windows!\n"); |
| 1487 |
|
| 1488 |
ret = init_irqdomain(host); |
| 1489 |
if (ret) |
| 1490 |
return dev_err_probe(dev, ret, "Failed to init IRQ domain\n"); |
| 1491 |
|
| 1492 |
ret = rzg3s_pcie_host_init(host); |
| 1493 |
if (ret) { |
| 1494 |
dev_err_probe(dev, ret, "Failed to initialize the HW!\n"); |
| 1495 |
goto teardown_irqdomain; |
| 1496 |
} |
| 1497 |
|
| 1498 |
ret = rzg3s_pcie_set_max_link_speed(host); |
| 1499 |
if (ret) |
| 1500 |
dev_info(dev, "Failed to set max link speed\n"); |
| 1501 |
|
| 1502 |
msleep(PCIE_RESET_CONFIG_WAIT_MS); |
| 1503 |
|
| 1504 |
return 0; |
| 1505 |
|
| 1506 |
teardown_irqdomain: |
| 1507 |
teardown_irqdomain(host); |
| 1508 |
|
| 1509 |
return ret; |
| 1510 |
} |
| 1511 |
|
| 1512 |
static int rzg3s_pcie_probe(struct platform_device *pdev) |
| 1513 |
{ |
| 1514 |
struct pci_host_bridge *bridge; |
| 1515 |
struct device *dev = &pdev->dev; |
| 1516 |
struct device_node *np = dev->of_node; |
| 1517 |
struct device_node *sysc_np __free(device_node) = |
| 1518 |
of_parse_phandle(np, "renesas,sysc", 0); |
| 1519 |
struct rzg3s_pcie_host *host; |
| 1520 |
int ret; |
| 1521 |
|
| 1522 |
bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host)); |
| 1523 |
if (!bridge) |
| 1524 |
return -ENOMEM; |
| 1525 |
|
| 1526 |
host = pci_host_bridge_priv(bridge); |
| 1527 |
host->dev = dev; |
| 1528 |
host->data = device_get_match_data(dev); |
| 1529 |
platform_set_drvdata(pdev, host); |
| 1530 |
|
| 1531 |
host->axi = devm_platform_ioremap_resource(pdev, 0); |
| 1532 |
if (IS_ERR(host->axi)) |
| 1533 |
return PTR_ERR(host->axi); |
| 1534 |
host->pcie = host->axi + RZG3S_PCI_CFG_BASE; |
| 1535 |
|
| 1536 |
host->max_link_speed = of_pci_get_max_link_speed(np); |
| 1537 |
if (host->max_link_speed < 0) |
| 1538 |
host->max_link_speed = 2; |
| 1539 |
|
| 1540 |
ret = rzg3s_pcie_host_parse_port(host); |
| 1541 |
if (ret) |
| 1542 |
return ret; |
| 1543 |
|
| 1544 |
host->sysc = syscon_node_to_regmap(sysc_np); |
| 1545 |
if (IS_ERR(host->sysc)) { |
| 1546 |
ret = PTR_ERR(host->sysc); |
| 1547 |
goto port_refclk_put; |
| 1548 |
} |
| 1549 |
|
| 1550 |
ret = regmap_update_bits(host->sysc, RZG3S_SYS_PCIE_RST_RSM_B, |
| 1551 |
RZG3S_SYS_PCIE_RST_RSM_B_MASK, |
| 1552 |
FIELD_PREP(RZG3S_SYS_PCIE_RST_RSM_B_MASK, 1)); |
| 1553 |
if (ret) |
| 1554 |
goto port_refclk_put; |
| 1555 |
|
| 1556 |
ret = rzg3s_pcie_resets_prepare_and_get(host); |
| 1557 |
if (ret) |
| 1558 |
goto sysc_signal_restore; |
| 1559 |
|
| 1560 |
ret = rzg3s_pcie_power_resets_deassert(host); |
| 1561 |
if (ret) |
| 1562 |
goto sysc_signal_restore; |
| 1563 |
|
| 1564 |
pm_runtime_enable(dev); |
| 1565 |
|
| 1566 |
/* |
| 1567 |
* Controller clocks are part of a clock power domain. Enable them |
| 1568 |
* through runtime PM. |
| 1569 |
*/ |
| 1570 |
ret = pm_runtime_resume_and_get(dev); |
| 1571 |
if (ret) |
| 1572 |
goto rpm_disable; |
| 1573 |
|
| 1574 |
raw_spin_lock_init(&host->hw_lock); |
| 1575 |
|
| 1576 |
ret = rzg3s_pcie_host_setup(host, rzg3s_pcie_init_irqdomain, |
| 1577 |
rzg3s_pcie_teardown_irqdomain); |
| 1578 |
if (ret) |
| 1579 |
goto rpm_put; |
| 1580 |
|
| 1581 |
bridge->sysdata = host; |
| 1582 |
bridge->ops = &rzg3s_pcie_root_ops; |
| 1583 |
bridge->child_ops = &rzg3s_pcie_child_ops; |
| 1584 |
ret = pci_host_probe(bridge); |
| 1585 |
if (ret) |
| 1586 |
goto host_probe_teardown; |
| 1587 |
|
| 1588 |
return 0; |
| 1589 |
|
| 1590 |
host_probe_teardown: |
| 1591 |
rzg3s_pcie_teardown_irqdomain(host); |
| 1592 |
reset_control_bulk_assert(host->data->num_cfg_resets, host->cfg_resets); |
| 1593 |
rpm_put: |
| 1594 |
pm_runtime_put_sync(dev); |
| 1595 |
rpm_disable: |
| 1596 |
pm_runtime_disable(dev); |
| 1597 |
reset_control_bulk_assert(host->data->num_power_resets, |
| 1598 |
host->power_resets); |
| 1599 |
sysc_signal_restore: |
| 1600 |
/* |
| 1601 |
* SYSC RST_RSM_B signal need to be asserted before turning off the |
| 1602 |
* power to the PHY. |
| 1603 |
*/ |
| 1604 |
regmap_update_bits(host->sysc, RZG3S_SYS_PCIE_RST_RSM_B, |
| 1605 |
RZG3S_SYS_PCIE_RST_RSM_B_MASK, |
| 1606 |
FIELD_PREP(RZG3S_SYS_PCIE_RST_RSM_B_MASK, 0)); |
| 1607 |
port_refclk_put: |
| 1608 |
clk_put(host->port.refclk); |
| 1609 |
|
| 1610 |
return ret; |
| 1611 |
} |
| 1612 |
|
| 1613 |
static int rzg3s_pcie_suspend_noirq(struct device *dev) |
| 1614 |
{ |
| 1615 |
struct rzg3s_pcie_host *host = dev_get_drvdata(dev); |
| 1616 |
const struct rzg3s_pcie_soc_data *data = host->data; |
| 1617 |
struct rzg3s_pcie_port *port = &host->port; |
| 1618 |
struct regmap *sysc = host->sysc; |
| 1619 |
int ret; |
| 1620 |
|
| 1621 |
ret = pm_runtime_put_sync(dev); |
| 1622 |
if (ret) |
| 1623 |
return ret; |
| 1624 |
|
| 1625 |
clk_disable_unprepare(port->refclk); |
| 1626 |
|
| 1627 |
ret = reset_control_bulk_assert(data->num_cfg_resets, |
| 1628 |
host->cfg_resets); |
| 1629 |
if (ret) |
| 1630 |
goto refclk_restore; |
| 1631 |
|
| 1632 |
ret = reset_control_bulk_assert(data->num_power_resets, |
| 1633 |
host->power_resets); |
| 1634 |
if (ret) |
| 1635 |
goto cfg_resets_restore; |
| 1636 |
|
| 1637 |
ret = regmap_update_bits(sysc, RZG3S_SYS_PCIE_RST_RSM_B, |
| 1638 |
RZG3S_SYS_PCIE_RST_RSM_B_MASK, |
| 1639 |
FIELD_PREP(RZG3S_SYS_PCIE_RST_RSM_B_MASK, 0)); |
| 1640 |
if (ret) |
| 1641 |
goto power_resets_restore; |
| 1642 |
|
| 1643 |
return 0; |
| 1644 |
|
| 1645 |
/* Restore the previous state if any error happens */ |
| 1646 |
power_resets_restore: |
| 1647 |
reset_control_bulk_deassert(data->num_power_resets, |
| 1648 |
host->power_resets); |
| 1649 |
cfg_resets_restore: |
| 1650 |
reset_control_bulk_deassert(data->num_cfg_resets, |
| 1651 |
host->cfg_resets); |
| 1652 |
refclk_restore: |
| 1653 |
clk_prepare_enable(port->refclk); |
| 1654 |
pm_runtime_resume_and_get(dev); |
| 1655 |
return ret; |
| 1656 |
} |
| 1657 |
|
| 1658 |
static int rzg3s_pcie_resume_noirq(struct device *dev) |
| 1659 |
{ |
| 1660 |
struct rzg3s_pcie_host *host = dev_get_drvdata(dev); |
| 1661 |
const struct rzg3s_pcie_soc_data *data = host->data; |
| 1662 |
struct regmap *sysc = host->sysc; |
| 1663 |
int ret; |
| 1664 |
|
| 1665 |
ret = regmap_update_bits(sysc, RZG3S_SYS_PCIE_RST_RSM_B, |
| 1666 |
RZG3S_SYS_PCIE_RST_RSM_B_MASK, |
| 1667 |
FIELD_PREP(RZG3S_SYS_PCIE_RST_RSM_B_MASK, 1)); |
| 1668 |
if (ret) |
| 1669 |
return ret; |
| 1670 |
|
| 1671 |
ret = rzg3s_pcie_power_resets_deassert(host); |
| 1672 |
if (ret) |
| 1673 |
goto assert_rst_rsm_b; |
| 1674 |
|
| 1675 |
ret = pm_runtime_resume_and_get(dev); |
| 1676 |
if (ret) |
| 1677 |
goto assert_power_resets; |
| 1678 |
|
| 1679 |
ret = rzg3s_pcie_host_setup(host, rzg3s_pcie_msi_hw_setup, |
| 1680 |
rzg3s_pcie_msi_hw_teardown); |
| 1681 |
if (ret) |
| 1682 |
goto rpm_put; |
| 1683 |
|
| 1684 |
return 0; |
| 1685 |
|
| 1686 |
/* |
| 1687 |
* If any error happens there is no way to recover the IP. Put it in the |
| 1688 |
* lowest possible power state. |
| 1689 |
*/ |
| 1690 |
rpm_put: |
| 1691 |
pm_runtime_put_sync(dev); |
| 1692 |
assert_power_resets: |
| 1693 |
reset_control_bulk_assert(data->num_power_resets, |
| 1694 |
host->power_resets); |
| 1695 |
assert_rst_rsm_b: |
| 1696 |
regmap_update_bits(sysc, RZG3S_SYS_PCIE_RST_RSM_B, |
| 1697 |
RZG3S_SYS_PCIE_RST_RSM_B_MASK, |
| 1698 |
FIELD_PREP(RZG3S_SYS_PCIE_RST_RSM_B_MASK, 0)); |
| 1699 |
return ret; |
| 1700 |
} |
| 1701 |
|
| 1702 |
static const struct dev_pm_ops rzg3s_pcie_pm_ops = { |
| 1703 |
NOIRQ_SYSTEM_SLEEP_PM_OPS(rzg3s_pcie_suspend_noirq, |
| 1704 |
rzg3s_pcie_resume_noirq) |
| 1705 |
}; |
| 1706 |
|
| 1707 |
static const char * const rzg3s_soc_power_resets[] = { |
| 1708 |
"aresetn", "rst_cfg_b", "rst_load_b", |
| 1709 |
}; |
| 1710 |
|
| 1711 |
static const char * const rzg3s_soc_cfg_resets[] = { |
| 1712 |
"rst_b", "rst_ps_b", "rst_gp_b", "rst_rsm_b", |
| 1713 |
}; |
| 1714 |
|
| 1715 |
static const struct rzg3s_pcie_soc_data rzg3s_soc_data = { |
| 1716 |
.power_resets = rzg3s_soc_power_resets, |
| 1717 |
.num_power_resets = ARRAY_SIZE(rzg3s_soc_power_resets), |
| 1718 |
.cfg_resets = rzg3s_soc_cfg_resets, |
| 1719 |
.num_cfg_resets = ARRAY_SIZE(rzg3s_soc_cfg_resets), |
| 1720 |
.init_phy = rzg3s_soc_pcie_init_phy, |
| 1721 |
}; |
| 1722 |
|
| 1723 |
static const struct of_device_id rzg3s_pcie_of_match[] = { |
| 1724 |
{ |
| 1725 |
.compatible = "renesas,r9a08g045-pcie", |
| 1726 |
.data = &rzg3s_soc_data, |
| 1727 |
}, |
| 1728 |
{} |
| 1729 |
}; |
| 1730 |
|
| 1731 |
static struct platform_driver rzg3s_pcie_driver = { |
| 1732 |
.driver = { |
| 1733 |
.name = "rzg3s-pcie-host", |
| 1734 |
.of_match_table = rzg3s_pcie_of_match, |
| 1735 |
.pm = pm_ptr(&rzg3s_pcie_pm_ops), |
| 1736 |
.suppress_bind_attrs = true, |
| 1737 |
.probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 1738 |
}, |
| 1739 |
.probe = rzg3s_pcie_probe, |
| 1740 |
}; |
| 1741 |
builtin_platform_driver(rzg3s_pcie_driver); |