1 // SPDX-License-Identifier: GPL-2.0+
13 #define DART_PARAMS2 0x0004
14 #define DART_PARAMS2_BYPASS_SUPPORT BIT(0)
16 #define DART_T8020_TLB_CMD 0x0020
17 #define DART_T8020_TLB_CMD_FLUSH BIT(20)
18 #define DART_T8020_TLB_CMD_BUSY BIT(2)
19 #define DART_T8020_TLB_SIDMASK 0x0034
20 #define DART_T8020_ERROR 0x0040
21 #define DART_T8020_ERROR_ADDR_LO 0x0050
22 #define DART_T8020_ERROR_ADDR_HI 0x0054
23 #define DART_T8020_CONFIG 0x0060
24 #define DART_T8020_CONFIG_LOCK BIT(15)
25 #define DART_T8020_SID_ENABLE 0x00fc
26 #define DART_T8020_TCR_BASE 0x0100
27 #define DART_T8020_TCR_TRANSLATE_ENABLE BIT(7)
28 #define DART_T8020_TCR_BYPASS_DART BIT(8)
29 #define DART_T8020_TCR_BYPASS_DAPF BIT(12)
30 #define DART_T8020_TTBR_BASE 0x0200
31 #define DART_T8020_TTBR_VALID BIT(31)
33 #define DART_T8110_PARAMS4 0x000c
34 #define DART_T8110_PARAMS4_NSID_MASK (0x1ff << 0)
35 #define DART_T8110_TLB_CMD 0x0080
36 #define DART_T8110_TLB_CMD_BUSY BIT(31)
37 #define DART_T8110_TLB_CMD_FLUSH_ALL BIT(8)
38 #define DART_T8110_ERROR 0x0100
39 #define DART_T8110_ERROR_MASK 0x0104
40 #define DART_T8110_ERROR_ADDR_LO 0x0170
41 #define DART_T8110_ERROR_ADDR_HI 0x0174
42 #define DART_T8110_PROTECT 0x0200
43 #define DART_T8110_PROTECT_TTBR_TCR BIT(0)
44 #define DART_T8110_SID_ENABLE_BASE 0x0c00
45 #define DART_T8110_TCR_BASE 0x1000
46 #define DART_T8110_TCR_BYPASS_DAPF BIT(2)
47 #define DART_T8110_TCR_BYPASS_DART BIT(1)
48 #define DART_T8110_TCR_TRANSLATE_ENABLE BIT(0)
49 #define DART_T8110_TTBR_BASE 0x1400
50 #define DART_T8110_TTBR_VALID BIT(0)
52 #define DART_SID_ENABLE(priv, idx) \
53 ((priv)->sid_enable_base + 4 * (idx))
54 #define DART_TCR(priv, sid) ((priv)->tcr_base + 4 * (sid))
55 #define DART_TTBR(priv, sid, idx) \
56 ((priv)->ttbr_base + 4 * (priv)->nttbr * (sid) + 4 * (idx))
57 #define DART_TTBR_SHIFT 12
59 #define DART_ALL_STREAMS(priv) ((1U << (priv)->nsid) - 1)
61 #define DART_PAGE_SIZE SZ_16K
62 #define DART_PAGE_MASK (DART_PAGE_SIZE - 1)
64 #define DART_L1_TABLE 0x3
65 #define DART_L2_INVAL 0
66 #define DART_L2_VALID BIT(0)
67 #define DART_L2_FULL_PAGE BIT(1)
68 #define DART_L2_START(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 52)
69 #define DART_L2_END(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 40)
71 struct apple_dart_priv {
84 u32 tcr_translate_enable;
88 void (*flush_tlb)(struct apple_dart_priv *priv);
91 static void apple_dart_t8020_flush_tlb(struct apple_dart_priv *priv)
95 writel(DART_ALL_STREAMS(priv), priv->base + DART_T8020_TLB_SIDMASK);
96 writel(DART_T8020_TLB_CMD_FLUSH, priv->base + DART_T8020_TLB_CMD);
97 while (readl(priv->base + DART_T8020_TLB_CMD) &
98 DART_T8020_TLB_CMD_BUSY)
102 static void apple_dart_t8110_flush_tlb(struct apple_dart_priv *priv)
106 writel(DART_T8110_TLB_CMD_FLUSH_ALL,
107 priv->base + DART_T8110_TLB_CMD_FLUSH_ALL);
108 while (readl(priv->base + DART_T8110_TLB_CMD) &
109 DART_T8110_TLB_CMD_BUSY)
113 static dma_addr_t apple_dart_map(struct udevice *dev, void *addr, size_t size)
115 struct apple_dart_priv *priv = dev_get_priv(dev);
116 phys_addr_t paddr, dva;
117 phys_size_t psize, off;
121 return (phys_addr_t)addr;
123 paddr = ALIGN_DOWN((phys_addr_t)addr, DART_PAGE_SIZE);
124 off = (phys_addr_t)addr - paddr;
125 psize = ALIGN(size + off, DART_PAGE_SIZE);
127 dva = lmb_alloc(&priv->lmb, psize, DART_PAGE_SIZE);
129 idx = dva / DART_PAGE_SIZE;
130 for (i = 0; i < psize / DART_PAGE_SIZE; i++) {
131 priv->l2[idx + i] = (paddr >> priv->shift) | DART_L2_VALID |
132 DART_L2_START(0LL) | DART_L2_END(~0LL);
133 paddr += DART_PAGE_SIZE;
135 flush_dcache_range((unsigned long)&priv->l2[idx],
136 (unsigned long)&priv->l2[idx + i]);
137 priv->flush_tlb(priv);
142 static void apple_dart_unmap(struct udevice *dev, dma_addr_t addr, size_t size)
144 struct apple_dart_priv *priv = dev_get_priv(dev);
152 dva = ALIGN_DOWN(addr, DART_PAGE_SIZE);
153 psize = size + (addr - dva);
154 psize = ALIGN(psize, DART_PAGE_SIZE);
156 idx = dva / DART_PAGE_SIZE;
157 for (i = 0; i < psize / DART_PAGE_SIZE; i++)
158 priv->l2[idx + i] = DART_L2_INVAL;
159 flush_dcache_range((unsigned long)&priv->l2[idx],
160 (unsigned long)&priv->l2[idx + i]);
161 priv->flush_tlb(priv);
163 lmb_free(&priv->lmb, dva, psize);
166 static struct iommu_ops apple_dart_ops = {
167 .map = apple_dart_map,
168 .unmap = apple_dart_unmap,
171 static int apple_dart_probe(struct udevice *dev)
173 struct apple_dart_priv *priv = dev_get_priv(dev);
178 u32 params2, params4;
180 priv->base = dev_read_addr_ptr(dev);
184 if (device_is_compatible(dev, "apple,t8110-dart")) {
185 params4 = readl(priv->base + DART_T8110_PARAMS4);
186 priv->nsid = params4 & DART_T8110_PARAMS4_NSID_MASK;
188 priv->sid_enable_base = DART_T8110_SID_ENABLE_BASE;
189 priv->tcr_base = DART_T8110_TCR_BASE;
190 priv->tcr_translate_enable = DART_T8110_TCR_TRANSLATE_ENABLE;
192 DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART;
193 priv->ttbr_base = DART_T8110_TTBR_BASE;
194 priv->ttbr_valid = DART_T8110_TTBR_VALID;
195 priv->flush_tlb = apple_dart_t8110_flush_tlb;
199 priv->sid_enable_base = DART_T8020_SID_ENABLE;
200 priv->tcr_base = DART_T8020_TCR_BASE;
201 priv->tcr_translate_enable = DART_T8020_TCR_TRANSLATE_ENABLE;
203 DART_T8020_TCR_BYPASS_DAPF | DART_T8020_TCR_BYPASS_DART;
204 priv->ttbr_base = DART_T8020_TTBR_BASE;
205 priv->ttbr_valid = DART_T8020_TTBR_VALID;
206 priv->flush_tlb = apple_dart_t8020_flush_tlb;
209 if (device_is_compatible(dev, "apple,t6000-dart") ||
210 device_is_compatible(dev, "apple,t8110-dart"))
213 priv->dvabase = DART_PAGE_SIZE;
214 priv->dvaend = SZ_4G - DART_PAGE_SIZE;
216 lmb_init(&priv->lmb);
217 lmb_add(&priv->lmb, priv->dvabase, priv->dvaend - priv->dvabase);
219 /* Disable translations. */
220 for (sid = 0; sid < priv->nsid; sid++)
221 writel(0, priv->base + DART_TCR(priv, sid));
223 /* Remove page tables. */
224 for (sid = 0; sid < priv->nsid; sid++) {
225 for (i = 0; i < priv->nttbr; i++)
226 writel(0, priv->base + DART_TTBR(priv, sid, i));
228 priv->flush_tlb(priv);
230 params2 = readl(priv->base + DART_PARAMS2);
231 if (params2 & DART_PARAMS2_BYPASS_SUPPORT) {
232 for (sid = 0; sid < priv->nsid; sid++) {
233 writel(priv->tcr_bypass,
234 priv->base + DART_TCR(priv, sid));
240 ntte = DIV_ROUND_UP(priv->dvaend, DART_PAGE_SIZE);
241 nl2 = DIV_ROUND_UP(ntte, DART_PAGE_SIZE / sizeof(u64));
242 nl1 = DIV_ROUND_UP(nl2, DART_PAGE_SIZE / sizeof(u64));
244 priv->l2 = memalign(DART_PAGE_SIZE, nl2 * DART_PAGE_SIZE);
245 memset(priv->l2, 0, nl2 * DART_PAGE_SIZE);
246 flush_dcache_range((unsigned long)priv->l2,
247 (unsigned long)priv->l2 + nl2 * DART_PAGE_SIZE);
249 priv->l1 = memalign(DART_PAGE_SIZE, nl1 * DART_PAGE_SIZE);
250 memset(priv->l1, 0, nl1 * DART_PAGE_SIZE);
251 l2 = (phys_addr_t)priv->l2;
252 for (i = 0; i < nl2; i++) {
253 priv->l1[i] = (l2 >> priv->shift) | DART_L1_TABLE;
254 l2 += DART_PAGE_SIZE;
256 flush_dcache_range((unsigned long)priv->l1,
257 (unsigned long)priv->l1 + nl1 * DART_PAGE_SIZE);
259 /* Install page tables. */
260 for (sid = 0; sid < priv->nsid; sid++) {
261 addr = (phys_addr_t)priv->l1;
262 for (i = 0; i < nl1; i++) {
263 writel(addr >> DART_TTBR_SHIFT | priv->ttbr_valid,
264 priv->base + DART_TTBR(priv, sid, i));
265 addr += DART_PAGE_SIZE;
268 priv->flush_tlb(priv);
270 /* Enable all streams. */
271 for (i = 0; i < priv->nsid / 32; i++)
272 writel(~0, priv->base + DART_SID_ENABLE(priv, i));
274 /* Enable translations. */
275 for (sid = 0; sid < priv->nsid; sid++) {
276 writel(priv->tcr_translate_enable,
277 priv->base + DART_TCR(priv, sid));
283 static int apple_dart_remove(struct udevice *dev)
285 struct apple_dart_priv *priv = dev_get_priv(dev);
288 /* Disable translations. */
289 for (sid = 0; sid < priv->nsid; sid++)
290 writel(0, priv->base + DART_TCR(priv, sid));
292 /* Remove page tables. */
293 for (sid = 0; sid < priv->nsid; sid++) {
294 for (i = 0; i < priv->nttbr; i++)
295 writel(0, priv->base + DART_TTBR(priv, sid, i));
297 priv->flush_tlb(priv);
302 static const struct udevice_id apple_dart_ids[] = {
303 { .compatible = "apple,t8103-dart" },
304 { .compatible = "apple,t6000-dart" },
305 { .compatible = "apple,t8110-dart" },
309 U_BOOT_DRIVER(apple_dart) = {
310 .name = "apple_dart",
312 .of_match = apple_dart_ids,
313 .priv_auto = sizeof(struct apple_dart_priv),
314 .ops = &apple_dart_ops,
315 .probe = apple_dart_probe,
316 .remove = apple_dart_remove,
317 .flags = DM_FLAG_OS_PREPARE