1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/net/wan/slic_ds26522.c
5 * Copyright (C) 2016 Freescale Semiconductor, Inc.
7 * Author:Zhao Qiang<qiang.zhao@nxp.com>
10 #include <linux/bitrev.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/kthread.h>
16 #include <linux/spi/spi.h>
17 #include <linux/wait.h>
18 #include <linux/param.h>
19 #include <linux/delay.h>
21 #include <linux/of_address.h>
23 #include "slic_ds26522.h"
25 #define SLIC_TRANS_LEN 1
26 #define SLIC_TWO_LEN 2
27 #define SLIC_THREE_LEN 3
29 static struct spi_device
*g_spi
;
31 MODULE_DESCRIPTION("Slic Maxim DS26522 driver");
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Zhao Qiang<B45475@freescale.com>");
35 /* the read/write format of address is
36 * w/r|A13|A12|A11|A10|A9|A8|A7|A6|A5|A4|A3|A2|A1|A0|x
38 static void slic_write(struct spi_device
*spi
, u16 addr
,
43 addr
= bitrev16(addr
) >> 1;
45 temp
[0] = (u8
)((addr
>> 8) & 0x7f);
46 temp
[1] = (u8
)(addr
& 0xfe);
49 /* write spi addr and value */
50 spi_write(spi
, &temp
[0], SLIC_THREE_LEN
);
53 static u8
slic_read(struct spi_device
*spi
, u16 addr
)
58 addr
= bitrev16(addr
) >> 1;
59 temp
[0] = (u8
)(((addr
>> 8) & 0x7f) | 0x80);
60 temp
[1] = (u8
)(addr
& 0xfe);
62 spi_write_then_read(spi
, &temp
[0], SLIC_TWO_LEN
, &data
,
69 static bool get_slic_product_code(struct spi_device
*spi
)
73 device_id
= slic_read(spi
, DS26522_IDR_ADDR
);
74 if ((device_id
& 0xf8) == 0x68)
80 static void ds26522_e1_spec_config(struct spi_device
*spi
)
82 /* Receive E1 Mode, Framer Disabled */
83 slic_write(spi
, DS26522_RMMR_ADDR
, DS26522_RMMR_E1
);
85 /* Transmit E1 Mode, Framer Disable */
86 slic_write(spi
, DS26522_TMMR_ADDR
, DS26522_TMMR_E1
);
88 /* Receive E1 Mode Framer Enable */
89 slic_write(spi
, DS26522_RMMR_ADDR
,
90 slic_read(spi
, DS26522_RMMR_ADDR
) | DS26522_RMMR_FRM_EN
);
92 /* Transmit E1 Mode Framer Enable */
93 slic_write(spi
, DS26522_TMMR_ADDR
,
94 slic_read(spi
, DS26522_TMMR_ADDR
) | DS26522_TMMR_FRM_EN
);
96 /* RCR1, receive E1 B8zs & ESF */
97 slic_write(spi
, DS26522_RCR1_ADDR
,
98 DS26522_RCR1_E1_HDB3
| DS26522_RCR1_E1_CCS
);
100 /* RSYSCLK=2.048MHz, RSYNC-Output */
101 slic_write(spi
, DS26522_RIOCR_ADDR
,
102 DS26522_RIOCR_2048KHZ
| DS26522_RIOCR_RSIO_OUT
);
104 /* TCR1 Transmit E1 b8zs */
105 slic_write(spi
, DS26522_TCR1_ADDR
, DS26522_TCR1_TB8ZS
);
107 /* TSYSCLK=2.048MHz, TSYNC-Output */
108 slic_write(spi
, DS26522_TIOCR_ADDR
,
109 DS26522_TIOCR_2048KHZ
| DS26522_TIOCR_TSIO_OUT
);
112 slic_write(spi
, DS26522_E1TAF_ADDR
, DS26522_E1TAF_DEFAULT
);
114 /* Set E1TNAF register */
115 slic_write(spi
, DS26522_E1TNAF_ADDR
, DS26522_E1TNAF_DEFAULT
);
117 /* Receive E1 Mode Framer Enable & init Done */
118 slic_write(spi
, DS26522_RMMR_ADDR
, slic_read(spi
, DS26522_RMMR_ADDR
) |
119 DS26522_RMMR_INIT_DONE
);
121 /* Transmit E1 Mode Framer Enable & init Done */
122 slic_write(spi
, DS26522_TMMR_ADDR
, slic_read(spi
, DS26522_TMMR_ADDR
) |
123 DS26522_TMMR_INIT_DONE
);
125 /* Configure LIU E1 mode */
126 slic_write(spi
, DS26522_LTRCR_ADDR
, DS26522_LTRCR_E1
);
128 /* E1 Mode default 75 ohm w/Transmit Impedance Matlinking */
129 slic_write(spi
, DS26522_LTITSR_ADDR
,
130 DS26522_LTITSR_TLIS_75OHM
| DS26522_LTITSR_LBOS_75OHM
);
132 /* E1 Mode default 75 ohm Long Haul w/Receive Impedance Matlinking */
133 slic_write(spi
, DS26522_LRISMR_ADDR
,
134 DS26522_LRISMR_75OHM
| DS26522_LRISMR_MAX
);
136 /* Enable Transmit output */
137 slic_write(spi
, DS26522_LMCR_ADDR
, DS26522_LMCR_TE
);
140 static int slic_ds26522_init_configure(struct spi_device
*spi
)
145 slic_write(spi
, DS26522_GTCCR_ADDR
, DS26522_GTCCR_BPREFSEL_REFCLKIN
|
146 DS26522_GTCCR_BFREQSEL_2048KHZ
|
147 DS26522_GTCCR_FREQSEL_2048KHZ
);
148 slic_write(spi
, DS26522_GTCR2_ADDR
, DS26522_GTCR2_TSSYNCOUT
);
149 slic_write(spi
, DS26522_GFCR_ADDR
, DS26522_GFCR_BPCLK_2048KHZ
);
152 slic_write(spi
, DS26522_GTCR1_ADDR
, DS26522_GTCR1
);
154 /* Global LIU Software Reset Register */
155 slic_write(spi
, DS26522_GLSRR_ADDR
, DS26522_GLSRR_RESET
);
157 /* Global Framer and BERT Software Reset Register */
158 slic_write(spi
, DS26522_GFSRR_ADDR
, DS26522_GFSRR_RESET
);
160 usleep_range(100, 120);
162 slic_write(spi
, DS26522_GLSRR_ADDR
, DS26522_GLSRR_NORMAL
);
163 slic_write(spi
, DS26522_GFSRR_ADDR
, DS26522_GFSRR_NORMAL
);
165 /* Perform RX/TX SRESET,Reset receiver */
166 slic_write(spi
, DS26522_RMMR_ADDR
, DS26522_RMMR_SFTRST
);
168 /* Reset tranceiver */
169 slic_write(spi
, DS26522_TMMR_ADDR
, DS26522_TMMR_SFTRST
);
171 usleep_range(100, 120);
173 /* Zero all Framer Registers */
174 for (addr
= DS26522_RF_ADDR_START
; addr
<= DS26522_RF_ADDR_END
;
176 slic_write(spi
, addr
, 0);
178 for (addr
= DS26522_TF_ADDR_START
; addr
<= DS26522_TF_ADDR_END
;
180 slic_write(spi
, addr
, 0);
182 for (addr
= DS26522_LIU_ADDR_START
; addr
<= DS26522_LIU_ADDR_END
;
184 slic_write(spi
, addr
, 0);
186 for (addr
= DS26522_BERT_ADDR_START
; addr
<= DS26522_BERT_ADDR_END
;
188 slic_write(spi
, addr
, 0);
190 /* setup ds26522 for E1 specification */
191 ds26522_e1_spec_config(spi
);
193 slic_write(spi
, DS26522_GTCR1_ADDR
, 0x00);
198 static void slic_ds26522_remove(struct spi_device
*spi
)
200 pr_info("DS26522 module uninstalled\n");
203 static int slic_ds26522_probe(struct spi_device
*spi
)
208 spi
->bits_per_word
= 8;
210 if (!get_slic_product_code(spi
))
213 ret
= slic_ds26522_init_configure(spi
);
215 pr_info("DS26522 cs%d configured\n", spi_get_chipselect(spi
, 0));
220 static const struct spi_device_id slic_ds26522_id
[] = {
221 { .name
= "ds26522" },
224 MODULE_DEVICE_TABLE(spi
, slic_ds26522_id
);
226 static const struct of_device_id slic_ds26522_match
[] = {
228 .compatible
= "maxim,ds26522",
232 MODULE_DEVICE_TABLE(of
, slic_ds26522_match
);
234 static struct spi_driver slic_ds26522_driver
= {
237 .bus
= &spi_bus_type
,
238 .of_match_table
= slic_ds26522_match
,
240 .probe
= slic_ds26522_probe
,
241 .remove
= slic_ds26522_remove
,
242 .id_table
= slic_ds26522_id
,
245 module_spi_driver(slic_ds26522_driver
);