blob: 7583699ef7b6a8be646b6ffaaee96da50946d9c5 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCIe driver for Renesas R-Car SoCs
Olivier Deprez157378f2022-04-04 15:47:50 +02004 * Copyright (C) 2014-2020 Renesas Electronics Europe Ltd
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 *
6 * Author: Phil Edworthy <phil.edworthy@renesas.com>
7 */
8
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009#include <linux/delay.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010#include <linux/pci.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011
Olivier Deprez157378f2022-04-04 15:47:50 +020012#include "pcie-rcar.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013
Olivier Deprez157378f2022-04-04 15:47:50 +020014void rcar_pci_write_reg(struct rcar_pcie *pcie, u32 val, unsigned int reg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015{
16 writel(val, pcie->base + reg);
17}
18
Olivier Deprez157378f2022-04-04 15:47:50 +020019u32 rcar_pci_read_reg(struct rcar_pcie *pcie, unsigned int reg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020{
21 return readl(pcie->base + reg);
22}
23
Olivier Deprez157378f2022-04-04 15:47:50 +020024void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025{
David Brazdil0f672f62019-12-10 10:32:29 +000026 unsigned int shift = BITS_PER_BYTE * (where & 3);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027 u32 val = rcar_pci_read_reg(pcie, where & ~3);
28
29 val &= ~(mask << shift);
30 val |= data << shift;
31 rcar_pci_write_reg(pcie, val, where & ~3);
32}
33
Olivier Deprez157378f2022-04-04 15:47:50 +020034int rcar_pcie_wait_for_phyrdy(struct rcar_pcie *pcie)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035{
Olivier Deprez157378f2022-04-04 15:47:50 +020036 unsigned int timeout = 10;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037
Olivier Deprez157378f2022-04-04 15:47:50 +020038 while (timeout--) {
39 if (rcar_pci_read_reg(pcie, PCIEPHYSR) & PHYRDY)
40 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041
Olivier Deprez157378f2022-04-04 15:47:50 +020042 msleep(5);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 }
44
Olivier Deprez157378f2022-04-04 15:47:50 +020045 return -ETIMEDOUT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046}
47
Olivier Deprez157378f2022-04-04 15:47:50 +020048int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049{
Olivier Deprez157378f2022-04-04 15:47:50 +020050 unsigned int timeout = 10000;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051
Olivier Deprez157378f2022-04-04 15:47:50 +020052 while (timeout--) {
53 if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
54 return 0;
55
56 udelay(5);
57 cpu_relax();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058 }
59
Olivier Deprez157378f2022-04-04 15:47:50 +020060 return -ETIMEDOUT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061}
62
Olivier Deprez157378f2022-04-04 15:47:50 +020063void rcar_pcie_set_outbound(struct rcar_pcie *pcie, int win,
64 struct resource_entry *window)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065{
66 /* Setup PCIe address space mappings for each resource */
Olivier Deprez0e641232021-09-23 10:07:05 +020067 struct resource *res = window->res;
Olivier Deprez157378f2022-04-04 15:47:50 +020068 resource_size_t res_start;
69 resource_size_t size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 u32 mask;
71
72 rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
73
74 /*
75 * The PAMR mask is calculated in units of 128Bytes, which
76 * keeps things pretty simple.
77 */
78 size = resource_size(res);
Olivier Deprez157378f2022-04-04 15:47:50 +020079 if (size > 128)
80 mask = (roundup_pow_of_two(size) / SZ_128) - 1;
81 else
82 mask = 0x0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
84
85 if (res->flags & IORESOURCE_IO)
Olivier Deprez0e641232021-09-23 10:07:05 +020086 res_start = pci_pio_to_address(res->start) - window->offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 else
Olivier Deprez0e641232021-09-23 10:07:05 +020088 res_start = res->start - window->offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089
90 rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win));
91 rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
92 PCIEPALR(win));
93
94 /* First resource is for IO */
95 mask = PAR_ENABLE;
96 if (res->flags & IORESOURCE_IO)
97 mask |= IO_SPACE;
98
99 rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
100}
101
Olivier Deprez157378f2022-04-04 15:47:50 +0200102void rcar_pcie_set_inbound(struct rcar_pcie *pcie, u64 cpu_addr,
103 u64 pci_addr, u64 flags, int idx, bool host)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104{
105 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200106 * Set up 64-bit inbound regions as the range parser doesn't
107 * distinguish between 32 and 64-bit types.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200109 if (host)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110 rcar_pci_write_reg(pcie, lower_32_bits(pci_addr),
111 PCIEPRAR(idx));
Olivier Deprez157378f2022-04-04 15:47:50 +0200112 rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
113 rcar_pci_write_reg(pcie, flags, PCIELAMR(idx));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114
Olivier Deprez157378f2022-04-04 15:47:50 +0200115 if (host)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116 rcar_pci_write_reg(pcie, upper_32_bits(pci_addr),
117 PCIEPRAR(idx + 1));
Olivier Deprez157378f2022-04-04 15:47:50 +0200118 rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx + 1));
119 rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120}