2 * drivers/net/wan/slic_ds26522.c
4 * Copyright (C) 2016 Freescale Semiconductor, Inc.
6 * Author:Zhao Qiang<qiang.zhao@nxp.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/bitrev.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/kthread.h>
20 #include <linux/spi/spi.h>
21 #include <linux/wait.h>
22 #include <linux/param.h>
23 #include <linux/delay.h>
25 #include <linux/of_address.h>
27 #include "slic_ds26522.h"
29 #define DRV_NAME "ds26522"
31 #define SLIC_TRANS_LEN 1
32 #define SLIC_TWO_LEN 2
33 #define SLIC_THREE_LEN 3
35 static struct spi_device *g_spi;
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Zhao Qiang<B45475@freescale.com>");
40 /* the read/write format of address is
41 * w/r|A13|A12|A11|A10|A9|A8|A7|A6|A5|A4|A3|A2|A1|A0|x
43 static void slic_write(struct spi_device *spi, u16 addr,
48 addr = bitrev16(addr) >> 1;
50 temp[0] = (u8)((addr >> 8) & 0x7f);
51 temp[1] = (u8)(addr & 0xfe);
54 /* write spi addr and value */
55 spi_write(spi, &temp[0], SLIC_THREE_LEN);
58 static u8 slic_read(struct spi_device *spi, u16 addr)
63 addr = bitrev16(addr) >> 1;
64 temp[0] = (u8)(((addr >> 8) & 0x7f) | 0x80);
65 temp[1] = (u8)(addr & 0xfe);
67 spi_write_then_read(spi, &temp[0], SLIC_TWO_LEN, &data,
74 static bool get_slic_product_code(struct spi_device *spi)
78 device_id = slic_read(spi, DS26522_IDR_ADDR);
79 if ((device_id & 0xf8) == 0x68)
85 static void ds26522_e1_spec_config(struct spi_device *spi)
87 /* Receive E1 Mode, Framer Disabled */
88 slic_write(spi, DS26522_RMMR_ADDR, DS26522_RMMR_E1);
90 /* Transmit E1 Mode, Framer Disable */
91 slic_write(spi, DS26522_TMMR_ADDR, DS26522_TMMR_E1);
93 /* Receive E1 Mode Framer Enable */
94 slic_write(spi, DS26522_RMMR_ADDR,
95 slic_read(spi, DS26522_RMMR_ADDR) | DS26522_RMMR_FRM_EN);
97 /* Transmit E1 Mode Framer Enable */
98 slic_write(spi, DS26522_TMMR_ADDR,
99 slic_read(spi, DS26522_TMMR_ADDR) | DS26522_TMMR_FRM_EN);
101 /* RCR1, receive E1 B8zs & ESF */
102 slic_write(spi, DS26522_RCR1_ADDR,
103 DS26522_RCR1_E1_HDB3 | DS26522_RCR1_E1_CCS);
105 /* RSYSCLK=2.048MHz, RSYNC-Output */
106 slic_write(spi, DS26522_RIOCR_ADDR,
107 DS26522_RIOCR_2048KHZ | DS26522_RIOCR_RSIO_OUT);
109 /* TCR1 Transmit E1 b8zs */
110 slic_write(spi, DS26522_TCR1_ADDR, DS26522_TCR1_TB8ZS);
112 /* TSYSCLK=2.048MHz, TSYNC-Output */
113 slic_write(spi, DS26522_TIOCR_ADDR,
114 DS26522_TIOCR_2048KHZ | DS26522_TIOCR_TSIO_OUT);
117 slic_write(spi, DS26522_E1TAF_ADDR, DS26522_E1TAF_DEFAULT);
119 /* Set E1TNAF register */
120 slic_write(spi, DS26522_E1TNAF_ADDR, DS26522_E1TNAF_DEFAULT);
122 /* Receive E1 Mode Framer Enable & init Done */
123 slic_write(spi, DS26522_RMMR_ADDR, slic_read(spi, DS26522_RMMR_ADDR) |
124 DS26522_RMMR_INIT_DONE);
126 /* Transmit E1 Mode Framer Enable & init Done */
127 slic_write(spi, DS26522_TMMR_ADDR, slic_read(spi, DS26522_TMMR_ADDR) |
128 DS26522_TMMR_INIT_DONE);
130 /* Configure LIU E1 mode */
131 slic_write(spi, DS26522_LTRCR_ADDR, DS26522_LTRCR_E1);
133 /* E1 Mode default 75 ohm w/Transmit Impedance Matlinking */
134 slic_write(spi, DS26522_LTITSR_ADDR,
135 DS26522_LTITSR_TLIS_75OHM | DS26522_LTITSR_LBOS_75OHM);
137 /* E1 Mode default 75 ohm Long Haul w/Receive Impedance Matlinking */
138 slic_write(spi, DS26522_LRISMR_ADDR,
139 DS26522_LRISMR_75OHM | DS26522_LRISMR_MAX);
141 /* Enable Transmit output */
142 slic_write(spi, DS26522_LMCR_ADDR, DS26522_LMCR_TE);
145 static int slic_ds26522_init_configure(struct spi_device *spi)
150 slic_write(spi, DS26522_GTCCR_ADDR, DS26522_GTCCR_BPREFSEL_REFCLKIN |
151 DS26522_GTCCR_BFREQSEL_2048KHZ |
152 DS26522_GTCCR_FREQSEL_2048KHZ);
153 slic_write(spi, DS26522_GTCR2_ADDR, DS26522_GTCR2_TSSYNCOUT);
154 slic_write(spi, DS26522_GFCR_ADDR, DS26522_GFCR_BPCLK_2048KHZ);
157 slic_write(spi, DS26522_GTCR1_ADDR, DS26522_GTCR1);
159 /* Global LIU Software Reset Register */
160 slic_write(spi, DS26522_GLSRR_ADDR, DS26522_GLSRR_RESET);
162 /* Global Framer and BERT Software Reset Register */
163 slic_write(spi, DS26522_GFSRR_ADDR, DS26522_GFSRR_RESET);
165 usleep_range(100, 120);
167 slic_write(spi, DS26522_GLSRR_ADDR, DS26522_GLSRR_NORMAL);
168 slic_write(spi, DS26522_GFSRR_ADDR, DS26522_GFSRR_NORMAL);
170 /* Perform RX/TX SRESET,Reset receiver */
171 slic_write(spi, DS26522_RMMR_ADDR, DS26522_RMMR_SFTRST);
173 /* Reset tranceiver */
174 slic_write(spi, DS26522_TMMR_ADDR, DS26522_TMMR_SFTRST);
176 usleep_range(100, 120);
178 /* Zero all Framer Registers */
179 for (addr = DS26522_RF_ADDR_START; addr <= DS26522_RF_ADDR_END;
181 slic_write(spi, addr, 0);
183 for (addr = DS26522_TF_ADDR_START; addr <= DS26522_TF_ADDR_END;
185 slic_write(spi, addr, 0);
187 for (addr = DS26522_LIU_ADDR_START; addr <= DS26522_LIU_ADDR_END;
189 slic_write(spi, addr, 0);
191 for (addr = DS26522_BERT_ADDR_START; addr <= DS26522_BERT_ADDR_END;
193 slic_write(spi, addr, 0);
195 /* setup ds26522 for E1 specification */
196 ds26522_e1_spec_config(spi);
198 slic_write(spi, DS26522_GTCR1_ADDR, 0x00);
203 static int slic_ds26522_remove(struct spi_device *spi)
205 pr_info("DS26522 module uninstalled\n");
209 static int slic_ds26522_probe(struct spi_device *spi)
214 spi->bits_per_word = 8;
216 if (!get_slic_product_code(spi))
219 ret = slic_ds26522_init_configure(spi);
221 pr_info("DS26522 cs%d configured\n", spi->chip_select);
226 static const struct spi_device_id slic_ds26522_id[] = {
227 { .name = "ds26522" },
230 MODULE_DEVICE_TABLE(spi, slic_ds26522_id);
232 static const struct of_device_id slic_ds26522_match[] = {
234 .compatible = "maxim,ds26522",
238 MODULE_DEVICE_TABLE(of, slic_ds26522_match);
240 static struct spi_driver slic_ds26522_driver = {
243 .bus = &spi_bus_type,
244 .of_match_table = slic_ds26522_match,
246 .probe = slic_ds26522_probe,
247 .remove = slic_ds26522_remove,
248 .id_table = slic_ds26522_id,
251 module_spi_driver(slic_ds26522_driver);