1 // SPDX-License-Identifier: GPL-2.0+159
3 * Take from dc tegra_ahub.c
5 * Copyright 2018 Google LLC
8 #define LOG_CATEGORY UCLASS_MISC
15 #include <asm/arch-tegra/tegra_ahub.h>
16 #include <asm/arch-tegra/tegra_i2s.h>
17 #include "tegra_i2s_priv.h"
19 struct tegra_ahub_priv {
20 struct apbif_regs *apbif_regs;
21 struct xbar_regs *xbar_regs;
23 int capacity_words; /* FIFO capacity in words */
26 * This is unset intially, but is set by tegra_ahub_ioctl() called
27 * from the misc_ioctl() in tegra_sound_probe()
33 static int tegra_ahub_xbar_enable_i2s(struct xbar_regs *regs, int i2s_id)
36 * Enables I2S as the receiver of APBIF by writing APBIF_TX0 (0x01) to
41 writel(1, ®s->i2s0_rx0);
44 writel(1, ®s->i2s1_rx0);
47 writel(1, ®s->i2s2_rx0);
50 writel(1, ®s->i2s3_rx0);
53 writel(1, ®s->i2s4_rx0);
56 log_err("Invalid I2S component id: %d\n", i2s_id);
62 static int tegra_ahub_apbif_is_full(struct udevice *dev)
64 struct tegra_ahub_priv *priv = dev_get_priv(dev);
66 return readl(&priv->apbif_regs->apbdma_live_stat) & priv->full_mask;
70 * tegra_ahub_wait_for_space() - Wait for space in the FIFO
72 * @return 0 if OK, -ETIMEDOUT if no space was available in time
74 static int tegra_ahub_wait_for_space(struct udevice *dev)
79 /* Busy-wait initially, since this should take almost no time */
81 if (!tegra_ahub_apbif_is_full(dev))
85 /* Failed, so do a slower loop for 100ms */
87 while (tegra_ahub_apbif_is_full(dev)) {
88 if (get_timer(start) > 100)
95 static int tegra_ahub_apbif_send(struct udevice *dev, int offset,
96 const void *buf, int len)
98 struct tegra_ahub_priv *priv = dev_get_priv(dev);
99 const u32 *data = (const u32 *)buf;
102 if (len % sizeof(*data)) {
103 log_err("Data size (%zd) must be aligned to %zd.\n", len,
107 while (written < len) {
108 int ret = tegra_ahub_wait_for_space(dev);
113 writel(*data++, &priv->apbif_regs->channel0_txfifo);
114 written += sizeof(*data);
120 static void tegra_ahub_apbif_set_cif(struct udevice *dev, u32 value)
122 struct tegra_ahub_priv *priv = dev_get_priv(dev);
124 writel(value, &priv->apbif_regs->channel0_cif_tx0_ctrl);
127 static void tegra_ahub_apbif_enable_channel0(struct udevice *dev,
130 struct tegra_ahub_priv *priv = dev_get_priv(dev);
132 u32 ctrl = TEGRA_AHUB_CHANNEL_CTRL_TX_PACK_EN |
133 TEGRA_AHUB_CHANNEL_CTRL_TX_PACK_16 |
134 TEGRA_AHUB_CHANNEL_CTRL_TX_EN;
136 fifo_threshold--; /* fifo_threshold starts from 1 */
137 ctrl |= (fifo_threshold << TEGRA_AHUB_CHANNEL_CTRL_TX_THRESHOLD_SHIFT);
138 writel(ctrl, &priv->apbif_regs->channel0_ctrl);
141 static u32 tegra_ahub_get_cif(bool is_receive, uint channels,
142 uint bits_per_sample, uint fifo_threshold)
144 uint audio_bits = (bits_per_sample >> 2) - 1;
147 channels--; /* Channels in CIF starts from 1 */
148 fifo_threshold--; /* FIFO threshold starts from 1 */
149 /* Assume input and output are always using same channel / bits */
150 val = channels << TEGRA_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT |
151 channels << TEGRA_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT |
152 audio_bits << TEGRA_AUDIOCIF_CTRL_AUDIO_BITS_SHIFT |
153 audio_bits << TEGRA_AUDIOCIF_CTRL_CLIENT_BITS_SHIFT |
154 fifo_threshold << TEGRA_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT |
155 (is_receive ? TEGRA_AUDIOCIF_DIRECTION_RX <<
156 TEGRA_AUDIOCIF_CTRL_DIRECTION_SHIFT : 0);
161 static int tegra_ahub_enable(struct udevice *dev)
163 struct tegra_ahub_priv *priv = dev_get_priv(dev);
164 struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(priv->i2s);
168 /* We use APBIF channel0 as a sender */
169 priv->full_mask = TEGRA_AHUB_APBDMA_LIVE_STATUS_CH0_TX_CIF_FIFO_FULL;
170 priv->capacity_words = 8;
173 * FIFO is inactive until (fifo_threshold) of words are sent. For
174 * better performance, we want to set it to half of capacity.
176 u32 fifo_threshold = priv->capacity_words / 2;
179 * Setup audio client interface (ACIF): APBIF (channel0) as sender and
182 cif_ctrl = tegra_ahub_get_cif(true, uc_priv->channels,
183 uc_priv->bitspersample, fifo_threshold);
184 tegra_i2s_set_cif_tx_ctrl(priv->i2s, cif_ctrl);
186 cif_ctrl = tegra_ahub_get_cif(false, uc_priv->channels,
187 uc_priv->bitspersample, fifo_threshold);
188 tegra_ahub_apbif_set_cif(dev, cif_ctrl);
189 tegra_ahub_apbif_enable_channel0(dev, fifo_threshold);
191 ret = tegra_ahub_xbar_enable_i2s(priv->xbar_regs, uc_priv->id);
194 log_debug("ahub: channels=%d, bitspersample=%d, cif_ctrl=%x, fifo_threshold=%d, id=%d\n",
195 uc_priv->channels, uc_priv->bitspersample, cif_ctrl,
196 fifo_threshold, uc_priv->id);
201 static int tegra_ahub_ioctl(struct udevice *dev, unsigned long request,
204 struct tegra_ahub_priv *priv = dev_get_priv(dev);
206 if (request != AHUB_MISCOP_SET_I2S)
209 priv->i2s = *(struct udevice **)buf;
210 log_debug("i2s set to '%s'\n", priv->i2s->name);
212 return tegra_ahub_enable(dev);
215 static int tegra_ahub_probe(struct udevice *dev)
217 struct tegra_ahub_priv *priv = dev_get_priv(dev);
220 addr = dev_read_addr_index(dev, 0);
221 if (addr == FDT_ADDR_T_NONE) {
222 log_debug("Invalid apbif address\n");
225 priv->apbif_regs = (struct apbif_regs *)addr;
227 addr = dev_read_addr_index(dev, 1);
228 if (addr == FDT_ADDR_T_NONE) {
229 log_debug("Invalid xbar address\n");
232 priv->xbar_regs = (struct xbar_regs *)addr;
233 log_debug("ahub apbif_regs=%p, xbar_regs=%p\n", priv->apbif_regs,
239 static struct misc_ops tegra_ahub_ops = {
240 .write = tegra_ahub_apbif_send,
241 .ioctl = tegra_ahub_ioctl,
244 static const struct udevice_id tegra_ahub_ids[] = {
245 { .compatible = "nvidia,tegra124-ahub" },
249 U_BOOT_DRIVER(tegra_ahub) = {
250 .name = "tegra_ahub",
252 .of_match = tegra_ahub_ids,
253 .ops = &tegra_ahub_ops,
254 .probe = tegra_ahub_probe,
255 .priv_auto_alloc_size = sizeof(struct tegra_ahub_priv),