12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
235ef1c20SZhao Qiang /*
335ef1c20SZhao Qiang * Copyright (C) 2015 Freescale Semiconductor, Inc. All rights reserved.
435ef1c20SZhao Qiang *
535ef1c20SZhao Qiang * Authors: Zhao Qiang <qiang.zhao@nxp.com>
635ef1c20SZhao Qiang *
735ef1c20SZhao Qiang * Description:
835ef1c20SZhao Qiang * QE TDM API Set - TDM specific routines implementations.
935ef1c20SZhao Qiang */
1035ef1c20SZhao Qiang #include <linux/io.h>
1135ef1c20SZhao Qiang #include <linux/kernel.h>
12*5df5b2e0SRob Herring #include <linux/of.h>
1335ef1c20SZhao Qiang #include <soc/fsl/qe/qe_tdm.h>
1435ef1c20SZhao Qiang
set_tdm_framer(const char * tdm_framer_type)1535ef1c20SZhao Qiang static int set_tdm_framer(const char *tdm_framer_type)
1635ef1c20SZhao Qiang {
1735ef1c20SZhao Qiang if (strcmp(tdm_framer_type, "e1") == 0)
1835ef1c20SZhao Qiang return TDM_FRAMER_E1;
1935ef1c20SZhao Qiang else if (strcmp(tdm_framer_type, "t1") == 0)
2035ef1c20SZhao Qiang return TDM_FRAMER_T1;
2135ef1c20SZhao Qiang else
2235ef1c20SZhao Qiang return -EINVAL;
2335ef1c20SZhao Qiang }
2435ef1c20SZhao Qiang
set_si_param(struct ucc_tdm * utdm,struct ucc_tdm_info * ut_info)2535ef1c20SZhao Qiang static void set_si_param(struct ucc_tdm *utdm, struct ucc_tdm_info *ut_info)
2635ef1c20SZhao Qiang {
2735ef1c20SZhao Qiang struct si_mode_info *si_info = &ut_info->si_info;
2835ef1c20SZhao Qiang
2935ef1c20SZhao Qiang if (utdm->tdm_mode == TDM_INTERNAL_LOOPBACK) {
3035ef1c20SZhao Qiang si_info->simr_crt = 1;
3135ef1c20SZhao Qiang si_info->simr_rfsd = 0;
3235ef1c20SZhao Qiang }
3335ef1c20SZhao Qiang }
3435ef1c20SZhao Qiang
ucc_of_parse_tdm(struct device_node * np,struct ucc_tdm * utdm,struct ucc_tdm_info * ut_info)3535ef1c20SZhao Qiang int ucc_of_parse_tdm(struct device_node *np, struct ucc_tdm *utdm,
3635ef1c20SZhao Qiang struct ucc_tdm_info *ut_info)
3735ef1c20SZhao Qiang {
3835ef1c20SZhao Qiang const char *sprop;
3935ef1c20SZhao Qiang int ret = 0;
4035ef1c20SZhao Qiang u32 val;
4135ef1c20SZhao Qiang
4235ef1c20SZhao Qiang sprop = of_get_property(np, "fsl,rx-sync-clock", NULL);
4335ef1c20SZhao Qiang if (sprop) {
4435ef1c20SZhao Qiang ut_info->uf_info.rx_sync = qe_clock_source(sprop);
4535ef1c20SZhao Qiang if ((ut_info->uf_info.rx_sync < QE_CLK_NONE) ||
4635ef1c20SZhao Qiang (ut_info->uf_info.rx_sync > QE_RSYNC_PIN)) {
4735ef1c20SZhao Qiang pr_err("QE-TDM: Invalid rx-sync-clock property\n");
4835ef1c20SZhao Qiang return -EINVAL;
4935ef1c20SZhao Qiang }
5035ef1c20SZhao Qiang } else {
5135ef1c20SZhao Qiang pr_err("QE-TDM: Invalid rx-sync-clock property\n");
5235ef1c20SZhao Qiang return -EINVAL;
5335ef1c20SZhao Qiang }
5435ef1c20SZhao Qiang
5535ef1c20SZhao Qiang sprop = of_get_property(np, "fsl,tx-sync-clock", NULL);
5635ef1c20SZhao Qiang if (sprop) {
5735ef1c20SZhao Qiang ut_info->uf_info.tx_sync = qe_clock_source(sprop);
5835ef1c20SZhao Qiang if ((ut_info->uf_info.tx_sync < QE_CLK_NONE) ||
5935ef1c20SZhao Qiang (ut_info->uf_info.tx_sync > QE_TSYNC_PIN)) {
6035ef1c20SZhao Qiang pr_err("QE-TDM: Invalid tx-sync-clock property\n");
6135ef1c20SZhao Qiang return -EINVAL;
6235ef1c20SZhao Qiang }
6335ef1c20SZhao Qiang } else {
6435ef1c20SZhao Qiang pr_err("QE-TDM: Invalid tx-sync-clock property\n");
6535ef1c20SZhao Qiang return -EINVAL;
6635ef1c20SZhao Qiang }
6735ef1c20SZhao Qiang
6835ef1c20SZhao Qiang ret = of_property_read_u32_index(np, "fsl,tx-timeslot-mask", 0, &val);
6935ef1c20SZhao Qiang if (ret) {
7035ef1c20SZhao Qiang pr_err("QE-TDM: Invalid tx-timeslot-mask property\n");
7135ef1c20SZhao Qiang return -EINVAL;
7235ef1c20SZhao Qiang }
7335ef1c20SZhao Qiang utdm->tx_ts_mask = val;
7435ef1c20SZhao Qiang
7535ef1c20SZhao Qiang ret = of_property_read_u32_index(np, "fsl,rx-timeslot-mask", 0, &val);
7635ef1c20SZhao Qiang if (ret) {
7735ef1c20SZhao Qiang ret = -EINVAL;
7835ef1c20SZhao Qiang pr_err("QE-TDM: Invalid rx-timeslot-mask property\n");
7935ef1c20SZhao Qiang return ret;
8035ef1c20SZhao Qiang }
8135ef1c20SZhao Qiang utdm->rx_ts_mask = val;
8235ef1c20SZhao Qiang
8335ef1c20SZhao Qiang ret = of_property_read_u32_index(np, "fsl,tdm-id", 0, &val);
8435ef1c20SZhao Qiang if (ret) {
8535ef1c20SZhao Qiang ret = -EINVAL;
8635ef1c20SZhao Qiang pr_err("QE-TDM: No fsl,tdm-id property for this UCC\n");
8735ef1c20SZhao Qiang return ret;
8835ef1c20SZhao Qiang }
8935ef1c20SZhao Qiang utdm->tdm_port = val;
9035ef1c20SZhao Qiang ut_info->uf_info.tdm_num = utdm->tdm_port;
9135ef1c20SZhao Qiang
9274c269f6SJulia Lawall if (of_property_read_bool(np, "fsl,tdm-internal-loopback"))
9335ef1c20SZhao Qiang utdm->tdm_mode = TDM_INTERNAL_LOOPBACK;
9435ef1c20SZhao Qiang else
9535ef1c20SZhao Qiang utdm->tdm_mode = TDM_NORMAL;
9635ef1c20SZhao Qiang
9735ef1c20SZhao Qiang sprop = of_get_property(np, "fsl,tdm-framer-type", NULL);
9835ef1c20SZhao Qiang if (!sprop) {
9935ef1c20SZhao Qiang ret = -EINVAL;
10035ef1c20SZhao Qiang pr_err("QE-TDM: No tdm-framer-type property for UCC\n");
10135ef1c20SZhao Qiang return ret;
10235ef1c20SZhao Qiang }
10335ef1c20SZhao Qiang ret = set_tdm_framer(sprop);
10435ef1c20SZhao Qiang if (ret < 0)
10535ef1c20SZhao Qiang return -EINVAL;
10635ef1c20SZhao Qiang utdm->tdm_framer_type = ret;
10735ef1c20SZhao Qiang
10835ef1c20SZhao Qiang ret = of_property_read_u32_index(np, "fsl,siram-entry-id", 0, &val);
10935ef1c20SZhao Qiang if (ret) {
11035ef1c20SZhao Qiang ret = -EINVAL;
11135ef1c20SZhao Qiang pr_err("QE-TDM: No siram entry id for UCC\n");
11235ef1c20SZhao Qiang return ret;
11335ef1c20SZhao Qiang }
11435ef1c20SZhao Qiang utdm->siram_entry_id = val;
11535ef1c20SZhao Qiang
11635ef1c20SZhao Qiang set_si_param(utdm, ut_info);
11735ef1c20SZhao Qiang return ret;
11835ef1c20SZhao Qiang }
1194ba25162SValentin Longchamp EXPORT_SYMBOL(ucc_of_parse_tdm);
12035ef1c20SZhao Qiang
ucc_tdm_init(struct ucc_tdm * utdm,struct ucc_tdm_info * ut_info)12135ef1c20SZhao Qiang void ucc_tdm_init(struct ucc_tdm *utdm, struct ucc_tdm_info *ut_info)
12235ef1c20SZhao Qiang {
12335ef1c20SZhao Qiang struct si1 __iomem *si_regs;
12435ef1c20SZhao Qiang u16 __iomem *siram;
12535ef1c20SZhao Qiang u16 siram_entry_valid;
12635ef1c20SZhao Qiang u16 siram_entry_closed;
12735ef1c20SZhao Qiang u16 ucc_num;
12835ef1c20SZhao Qiang u8 csel;
12935ef1c20SZhao Qiang u16 sixmr;
13035ef1c20SZhao Qiang u16 tdm_port;
13135ef1c20SZhao Qiang u32 siram_entry_id;
13235ef1c20SZhao Qiang u32 mask;
13335ef1c20SZhao Qiang int i;
13435ef1c20SZhao Qiang
13535ef1c20SZhao Qiang si_regs = utdm->si_regs;
13635ef1c20SZhao Qiang siram = utdm->siram;
13735ef1c20SZhao Qiang ucc_num = ut_info->uf_info.ucc_num;
13835ef1c20SZhao Qiang tdm_port = utdm->tdm_port;
13935ef1c20SZhao Qiang siram_entry_id = utdm->siram_entry_id;
14035ef1c20SZhao Qiang
14135ef1c20SZhao Qiang if (utdm->tdm_framer_type == TDM_FRAMER_T1)
14235ef1c20SZhao Qiang utdm->num_of_ts = 24;
14335ef1c20SZhao Qiang if (utdm->tdm_framer_type == TDM_FRAMER_E1)
14435ef1c20SZhao Qiang utdm->num_of_ts = 32;
14535ef1c20SZhao Qiang
14635ef1c20SZhao Qiang /* set siram table */
14735ef1c20SZhao Qiang csel = (ucc_num < 4) ? ucc_num + 9 : ucc_num - 3;
14835ef1c20SZhao Qiang
14935ef1c20SZhao Qiang siram_entry_valid = SIR_CSEL(csel) | SIR_BYTE | SIR_CNT(0);
15035ef1c20SZhao Qiang siram_entry_closed = SIR_IDLE | SIR_BYTE | SIR_CNT(0);
15135ef1c20SZhao Qiang
15235ef1c20SZhao Qiang for (i = 0; i < utdm->num_of_ts; i++) {
15335ef1c20SZhao Qiang mask = 0x01 << i;
15435ef1c20SZhao Qiang
15535ef1c20SZhao Qiang if (utdm->tx_ts_mask & mask)
15635ef1c20SZhao Qiang iowrite16be(siram_entry_valid,
15735ef1c20SZhao Qiang &siram[siram_entry_id * 32 + i]);
15835ef1c20SZhao Qiang else
15935ef1c20SZhao Qiang iowrite16be(siram_entry_closed,
16035ef1c20SZhao Qiang &siram[siram_entry_id * 32 + i]);
16135ef1c20SZhao Qiang
16235ef1c20SZhao Qiang if (utdm->rx_ts_mask & mask)
16335ef1c20SZhao Qiang iowrite16be(siram_entry_valid,
16435ef1c20SZhao Qiang &siram[siram_entry_id * 32 + 0x200 + i]);
16535ef1c20SZhao Qiang else
16635ef1c20SZhao Qiang iowrite16be(siram_entry_closed,
16735ef1c20SZhao Qiang &siram[siram_entry_id * 32 + 0x200 + i]);
16835ef1c20SZhao Qiang }
16935ef1c20SZhao Qiang
17077d7676aSRasmus Villemoes qe_setbits_be16(&siram[(siram_entry_id * 32) + (utdm->num_of_ts - 1)],
17135ef1c20SZhao Qiang SIR_LAST);
17277d7676aSRasmus Villemoes qe_setbits_be16(&siram[(siram_entry_id * 32) + 0x200 + (utdm->num_of_ts - 1)],
17335ef1c20SZhao Qiang SIR_LAST);
17435ef1c20SZhao Qiang
17535ef1c20SZhao Qiang /* Set SIxMR register */
17635ef1c20SZhao Qiang sixmr = SIMR_SAD(siram_entry_id);
17735ef1c20SZhao Qiang
17835ef1c20SZhao Qiang sixmr &= ~SIMR_SDM_MASK;
17935ef1c20SZhao Qiang
18035ef1c20SZhao Qiang if (utdm->tdm_mode == TDM_INTERNAL_LOOPBACK)
18135ef1c20SZhao Qiang sixmr |= SIMR_SDM_INTERNAL_LOOPBACK;
18235ef1c20SZhao Qiang else
18335ef1c20SZhao Qiang sixmr |= SIMR_SDM_NORMAL;
18435ef1c20SZhao Qiang
18535ef1c20SZhao Qiang sixmr |= SIMR_RFSD(ut_info->si_info.simr_rfsd) |
18635ef1c20SZhao Qiang SIMR_TFSD(ut_info->si_info.simr_tfsd);
18735ef1c20SZhao Qiang
18835ef1c20SZhao Qiang if (ut_info->si_info.simr_crt)
18935ef1c20SZhao Qiang sixmr |= SIMR_CRT;
19035ef1c20SZhao Qiang if (ut_info->si_info.simr_sl)
19135ef1c20SZhao Qiang sixmr |= SIMR_SL;
19235ef1c20SZhao Qiang if (ut_info->si_info.simr_ce)
19335ef1c20SZhao Qiang sixmr |= SIMR_CE;
19435ef1c20SZhao Qiang if (ut_info->si_info.simr_fe)
19535ef1c20SZhao Qiang sixmr |= SIMR_FE;
19635ef1c20SZhao Qiang if (ut_info->si_info.simr_gm)
19735ef1c20SZhao Qiang sixmr |= SIMR_GM;
19835ef1c20SZhao Qiang
19935ef1c20SZhao Qiang switch (tdm_port) {
20035ef1c20SZhao Qiang case 0:
20135ef1c20SZhao Qiang iowrite16be(sixmr, &si_regs->sixmr1[0]);
20235ef1c20SZhao Qiang break;
20335ef1c20SZhao Qiang case 1:
20435ef1c20SZhao Qiang iowrite16be(sixmr, &si_regs->sixmr1[1]);
20535ef1c20SZhao Qiang break;
20635ef1c20SZhao Qiang case 2:
20735ef1c20SZhao Qiang iowrite16be(sixmr, &si_regs->sixmr1[2]);
20835ef1c20SZhao Qiang break;
20935ef1c20SZhao Qiang case 3:
21035ef1c20SZhao Qiang iowrite16be(sixmr, &si_regs->sixmr1[3]);
21135ef1c20SZhao Qiang break;
21235ef1c20SZhao Qiang default:
21335ef1c20SZhao Qiang pr_err("QE-TDM: can not find tdm sixmr reg\n");
21435ef1c20SZhao Qiang break;
21535ef1c20SZhao Qiang }
21635ef1c20SZhao Qiang }
2174ba25162SValentin Longchamp EXPORT_SYMBOL(ucc_tdm_init);
218