blob: 25cd82f16ebeff224a59e1faea1aa5baba363b3a [file] [log] [blame]
Laurentiu Tudor512d13e2018-08-09 15:19:46 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 */
5
6#include <common.h>
7#include <linux/libfdt.h>
8#include <fdt_support.h>
9
10#include <asm/io.h>
11#include <asm/processor.h>
12#include <asm/arch-fsl-layerscape/fsl_icid.h>
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +030013#include <fsl_fman.h>
Laurentiu Tudor512d13e2018-08-09 15:19:46 +030014
15static void set_icid(struct icid_id_table *tbl, int size)
16{
17 int i;
18
19 for (i = 0; i < size; i++)
Laurentiu Tudor7c326ac2019-07-30 17:29:57 +030020 if (tbl[i].le)
21 out_le32((u32 *)(tbl[i].reg_addr), tbl[i].reg);
22 else
23 out_be32((u32 *)(tbl[i].reg_addr), tbl[i].reg);
Laurentiu Tudor512d13e2018-08-09 15:19:46 +030024}
25
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +030026#ifdef CONFIG_SYS_DPAA_FMAN
27void set_fman_icids(struct fman_icid_id_table *tbl, int size)
28{
29 int i;
30 ccsr_fman_t *fm = (void *)CONFIG_SYS_FSL_FM1_ADDR;
31
32 for (i = 0; i < size; i++) {
33 out_be32(&fm->fm_bmi_common.fmbm_ppid[tbl[i].port_id - 1],
34 tbl[i].icid);
35 }
36}
37#endif
38
Laurentiu Tudor512d13e2018-08-09 15:19:46 +030039void set_icids(void)
40{
41 /* setup general icid offsets */
42 set_icid(icid_tbl, icid_tbl_sz);
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +030043
44#ifdef CONFIG_SYS_DPAA_FMAN
45 set_fman_icids(fman_icid_tbl, fman_icid_tbl_sz);
46#endif
Laurentiu Tudor512d13e2018-08-09 15:19:46 +030047}
48
49int fdt_set_iommu_prop(void *blob, int off, int smmu_ph, u32 *ids, int num_ids)
50{
51 int i, ret;
52 u32 prop[8];
53
54 /*
55 * Note: The "iommus" property definition mentions Stream IDs while
56 * this code handles ICIDs. The current implementation assumes that
57 * ICIDs and Stream IDs are equal.
58 */
59 for (i = 0; i < num_ids; i++) {
60 prop[i * 2] = cpu_to_fdt32(smmu_ph);
61 prop[i * 2 + 1] = cpu_to_fdt32(ids[i]);
62 }
63 ret = fdt_setprop(blob, off, "iommus",
64 prop, sizeof(u32) * num_ids * 2);
65 if (ret) {
66 printf("WARNING unable to set iommus: %s\n", fdt_strerror(ret));
67 return ret;
68 }
69
70 return 0;
71}
72
73int fdt_fixup_icid_tbl(void *blob, int smmu_ph,
74 struct icid_id_table *tbl, int size)
75{
76 int i, err, off;
77
78 for (i = 0; i < size; i++) {
79 if (!tbl[i].compat)
80 continue;
81
82 off = fdt_node_offset_by_compat_reg(blob,
83 tbl[i].compat,
84 tbl[i].compat_addr);
85 if (off > 0) {
86 err = fdt_set_iommu_prop(blob, off, smmu_ph,
87 &tbl[i].id, 1);
88 if (err)
89 return err;
90 } else {
91 printf("WARNING could not find node %s: %s.\n",
92 tbl[i].compat, fdt_strerror(off));
93 }
94 }
95
96 return 0;
97}
98
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +030099#ifdef CONFIG_SYS_DPAA_FMAN
100int get_fman_port_icid(int port_id, struct fman_icid_id_table *tbl,
101 const int size)
102{
103 int i;
104
105 for (i = 0; i < size; i++) {
106 if (tbl[i].port_id == port_id)
107 return tbl[i].icid;
108 }
109
110 return -1;
111}
112
113void fdt_fixup_fman_port_icid_by_compat(void *blob, int smmu_ph,
114 const char *compat)
115{
116 int noff, len, icid;
117 const u32 *prop;
118
Marek BehĂșn5d6b4482022-01-20 01:04:42 +0100119 fdt_for_each_node_by_compatible(noff, blob, -1, compat) {
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +0300120 prop = fdt_getprop(blob, noff, "cell-index", &len);
121 if (!prop) {
122 printf("WARNING missing cell-index for fman port\n");
123 continue;
124 }
125 if (len != 4) {
126 printf("WARNING bad cell-index size for fman port\n");
127 continue;
128 }
129
130 icid = get_fman_port_icid(fdt32_to_cpu(*prop),
131 fman_icid_tbl, fman_icid_tbl_sz);
132 if (icid < 0) {
133 printf("WARNING unknown ICID for fman port %d\n",
134 *prop);
135 continue;
136 }
137
138 fdt_set_iommu_prop(blob, noff, smmu_ph, (u32 *)&icid, 1);
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +0300139 }
140}
141
142void fdt_fixup_fman_icids(void *blob, int smmu_ph)
143{
144 static const char * const compats[] = {
145 "fsl,fman-v3-port-oh",
146 "fsl,fman-v3-port-rx",
147 "fsl,fman-v3-port-tx",
148 };
149 int i;
150
151 for (i = 0; i < ARRAY_SIZE(compats); i++)
152 fdt_fixup_fman_port_icid_by_compat(blob, smmu_ph, compats[i]);
153}
154#endif
155
Laurentiu Tudor512d13e2018-08-09 15:19:46 +0300156int fdt_get_smmu_phandle(void *blob)
157{
158 int noff, smmu_ph;
159
160 noff = fdt_node_offset_by_compatible(blob, -1, "arm,mmu-500");
161 if (noff < 0) {
162 printf("WARNING failed to get smmu node: %s\n",
163 fdt_strerror(noff));
164 return noff;
165 }
166
167 smmu_ph = fdt_get_phandle(blob, noff);
168 if (!smmu_ph) {
169 smmu_ph = fdt_create_phandle(blob, noff);
170 if (!smmu_ph) {
171 printf("WARNING failed to get smmu phandle\n");
172 return -1;
173 }
174 }
175
176 return smmu_ph;
177}
178
179void fdt_fixup_icid(void *blob)
180{
181 int smmu_ph;
182
183 smmu_ph = fdt_get_smmu_phandle(blob);
184 if (smmu_ph < 0)
185 return;
186
187 fdt_fixup_icid_tbl(blob, smmu_ph, icid_tbl, icid_tbl_sz);
Laurentiu Tudordcb7f3d2018-08-09 15:19:48 +0300188
189#ifdef CONFIG_SYS_DPAA_FMAN
190 fdt_fixup_fman_icids(blob, smmu_ph);
191#endif
Laurentiu Tudor512d13e2018-08-09 15:19:46 +0300192}