1 // SPDX-License-Identifier: GPL-2.0+
13 #include <asm/arch/rtkit.h>
14 #include <asm/arch/sart.h>
15 #include <linux/iopoll.h>
18 #define REG_CPU_CTRL 0x0044
19 #define REG_CPU_CTRL_RUN BIT(4)
21 /* Apple NVMe registers */
22 #define ANS_MAX_PEND_CMDS_CTRL 0x01210
23 #define ANS_MAX_QUEUE_DEPTH 64
24 #define ANS_BOOT_STATUS 0x01300
25 #define ANS_BOOT_STATUS_OK 0xde71ce55
26 #define ANS_MODESEL 0x01304
27 #define ANS_UNKNOWN_CTRL 0x24008
28 #define ANS_PRP_NULL_CHECK (1 << 11)
29 #define ANS_LINEAR_SQ_CTRL 0x24908
30 #define ANS_LINEAR_SQ_CTRL_EN (1 << 0)
31 #define ANS_ASQ_DB 0x2490c
32 #define ANS_IOSQ_DB 0x24910
33 #define ANS_NVMMU_NUM 0x28100
34 #define ANS_NVMMU_BASE_ASQ 0x28108
35 #define ANS_NVMMU_BASE_IOSQ 0x28110
36 #define ANS_NVMMU_TCB_INVAL 0x28118
37 #define ANS_NVMMU_TCB_STAT 0x28120
39 #define ANS_NVMMU_TCB_SIZE 0x4000
40 #define ANS_NVMMU_TCB_PITCH 0x80
43 * The Apple NVMe controller includes an IOMMU known as NVMMU. The
44 * NVMMU is programmed through an array of TCBs. These TCBs are paired
45 * with the corresponding slot in the submission queues and need to be
46 * configured with the command details before a command is allowed to
47 * execute. This is necessary even for commands that don't do DMA.
49 struct ans_nvmmu_tcb {
60 #define ANS_NVMMU_TCB_WRITE BIT(0)
61 #define ANS_NVMMU_TCB_READ BIT(1)
63 struct apple_nvme_priv {
65 void *base; /* NVMe registers */
66 void *asc; /* ASC registers */
67 struct reset_ctl_bulk resets; /* ASC reset */
68 struct mbox_chan chan;
69 struct apple_sart *sart;
70 struct apple_rtkit *rtk;
71 struct ans_nvmmu_tcb *tcbs[NVME_Q_NUM]; /* Submission queue TCBs */
72 u32 __iomem *q_db[NVME_Q_NUM]; /* Submission queue doorbell */
75 static int apple_nvme_setup_queue(struct nvme_queue *nvmeq)
77 struct apple_nvme_priv *priv =
78 container_of(nvmeq->dev, struct apple_nvme_priv, ndev);
79 struct nvme_dev *dev = nvmeq->dev;
89 priv->tcbs[nvmeq->qid] = (void *)memalign(4096, ANS_NVMMU_TCB_SIZE);
90 memset((void *)priv->tcbs[nvmeq->qid], 0, ANS_NVMMU_TCB_SIZE);
94 priv->q_db[nvmeq->qid] =
95 ((void __iomem *)dev->bar) + ANS_ASQ_DB;
96 nvme_writeq((ulong)priv->tcbs[nvmeq->qid],
97 ((void __iomem *)dev->bar) + ANS_NVMMU_BASE_ASQ);
100 priv->q_db[nvmeq->qid] =
101 ((void __iomem *)dev->bar) + ANS_IOSQ_DB;
102 nvme_writeq((ulong)priv->tcbs[nvmeq->qid],
103 ((void __iomem *)dev->bar) + ANS_NVMMU_BASE_IOSQ);
110 static void apple_nvme_submit_cmd(struct nvme_queue *nvmeq,
111 struct nvme_command *cmd)
113 struct apple_nvme_priv *priv =
114 container_of(nvmeq->dev, struct apple_nvme_priv, ndev);
115 struct ans_nvmmu_tcb *tcb;
116 u16 tail = nvmeq->sq_tail;
118 tcb = ((void *)priv->tcbs[nvmeq->qid]) + tail * ANS_NVMMU_TCB_PITCH;
119 memset(tcb, 0, sizeof(*tcb));
120 tcb->opcode = cmd->common.opcode;
121 tcb->flags = ANS_NVMMU_TCB_WRITE | ANS_NVMMU_TCB_READ;
123 tcb->prpl_len = cmd->rw.length;
124 tcb->prp1 = cmd->common.prp1;
125 tcb->prp2 = cmd->common.prp2;
127 writel(tail, priv->q_db[nvmeq->qid]);
130 static void apple_nvme_complete_cmd(struct nvme_queue *nvmeq,
131 struct nvme_command *cmd)
133 struct apple_nvme_priv *priv =
134 container_of(nvmeq->dev, struct apple_nvme_priv, ndev);
135 struct ans_nvmmu_tcb *tcb;
136 u16 tail = nvmeq->sq_tail;
138 tcb = ((void *)priv->tcbs[nvmeq->qid]) + tail * ANS_NVMMU_TCB_PITCH;
139 memset(tcb, 0, sizeof(*tcb));
140 writel(tail, ((void __iomem *)nvmeq->dev->bar) + ANS_NVMMU_TCB_INVAL);
141 readl(((void __iomem *)nvmeq->dev->bar) + ANS_NVMMU_TCB_STAT);
143 if (++tail == nvmeq->q_depth)
145 nvmeq->sq_tail = tail;
148 static int nvme_shmem_setup(void *cookie, struct apple_rtkit_buffer *buf)
150 struct apple_nvme_priv *priv = (struct apple_nvme_priv *)cookie;
152 if (!buf || buf->dva || !buf->size)
155 buf->buffer = memalign(SZ_16K, ALIGN(buf->size, SZ_16K));
159 if (!sart_add_allowed_region(priv->sart, buf->buffer, buf->size)) {
166 buf->dva = (u64)buf->buffer;
171 static void nvme_shmem_destroy(void *cookie, struct apple_rtkit_buffer *buf)
173 struct apple_nvme_priv *priv = (struct apple_nvme_priv *)cookie;
179 sart_remove_allowed_region(priv->sart, buf->buffer, buf->size);
187 static int apple_nvme_probe(struct udevice *dev)
189 struct apple_nvme_priv *priv = dev_get_priv(dev);
192 u32 ctrl, stat, phandle;
195 priv->base = dev_read_addr_ptr(dev);
199 addr = dev_read_addr_index(dev, 1);
200 if (addr == FDT_ADDR_T_NONE)
202 priv->asc = map_sysmem(addr, 0);
204 ret = reset_get_bulk(dev, &priv->resets);
208 ret = mbox_get_by_index(dev, 0, &priv->chan);
212 ret = dev_read_u32(dev, "apple,sart", &phandle);
216 of_sart = ofnode_get_by_phandle(phandle);
217 priv->sart = sart_init(of_sart);
221 ctrl = readl(priv->asc + REG_CPU_CTRL);
222 writel(ctrl | REG_CPU_CTRL_RUN, priv->asc + REG_CPU_CTRL);
224 priv->rtk = apple_rtkit_init(&priv->chan, priv, nvme_shmem_setup, nvme_shmem_destroy);
228 ret = apple_rtkit_boot(priv->rtk);
230 printf("%s: NVMe apple_rtkit_boot returned: %d\n", __func__, ret);
234 ret = readl_poll_sleep_timeout(priv->base + ANS_BOOT_STATUS, stat,
235 (stat == ANS_BOOT_STATUS_OK), 100,
238 printf("%s: NVMe firmware didn't boot\n", __func__);
242 writel(ANS_LINEAR_SQ_CTRL_EN, priv->base + ANS_LINEAR_SQ_CTRL);
243 writel(((ANS_MAX_QUEUE_DEPTH << 16) | ANS_MAX_QUEUE_DEPTH),
244 priv->base + ANS_MAX_PEND_CMDS_CTRL);
246 writel(readl(priv->base + ANS_UNKNOWN_CTRL) & ~ANS_PRP_NULL_CHECK,
247 priv->base + ANS_UNKNOWN_CTRL);
249 strcpy(priv->ndev.vendor, "Apple");
251 writel((ANS_NVMMU_TCB_SIZE / ANS_NVMMU_TCB_PITCH) - 1,
252 priv->base + ANS_NVMMU_NUM);
253 writel(0, priv->base + ANS_MODESEL);
255 priv->ndev.bar = priv->base;
256 return nvme_init(dev);
259 static int apple_nvme_remove(struct udevice *dev)
261 struct apple_nvme_priv *priv = dev_get_priv(dev);
266 apple_rtkit_shutdown(priv->rtk, APPLE_RTKIT_PWR_STATE_SLEEP);
268 ctrl = readl(priv->asc + REG_CPU_CTRL);
269 writel(ctrl & ~REG_CPU_CTRL_RUN, priv->asc + REG_CPU_CTRL);
271 apple_rtkit_free(priv->rtk);
274 sart_free(priv->sart);
277 reset_assert_bulk(&priv->resets);
278 reset_deassert_bulk(&priv->resets);
283 static const struct nvme_ops apple_nvme_ops = {
284 .setup_queue = apple_nvme_setup_queue,
285 .submit_cmd = apple_nvme_submit_cmd,
286 .complete_cmd = apple_nvme_complete_cmd,
289 static const struct udevice_id apple_nvme_ids[] = {
290 { .compatible = "apple,nvme-ans2" },
294 U_BOOT_DRIVER(apple_nvme) = {
295 .name = "apple_nvme",
297 .of_match = apple_nvme_ids,
298 .priv_auto = sizeof(struct apple_nvme_priv),
299 .probe = apple_nvme_probe,
300 .remove = apple_nvme_remove,
301 .ops = &apple_nvme_ops,
302 .flags = DM_FLAG_OS_PREPARE,