18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * drivers/net/wan/slic_ds26522.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Freescale Semiconductor, Inc. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author:Zhao Qiang<qiang.zhao@nxp.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/bitrev.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <linux/kernel.h> 148c2ecf20Sopenharmony_ci#include <linux/sched.h> 158c2ecf20Sopenharmony_ci#include <linux/kthread.h> 168c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 178c2ecf20Sopenharmony_ci#include <linux/wait.h> 188c2ecf20Sopenharmony_ci#include <linux/param.h> 198c2ecf20Sopenharmony_ci#include <linux/delay.h> 208c2ecf20Sopenharmony_ci#include <linux/of.h> 218c2ecf20Sopenharmony_ci#include <linux/of_address.h> 228c2ecf20Sopenharmony_ci#include <linux/io.h> 238c2ecf20Sopenharmony_ci#include "slic_ds26522.h" 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define SLIC_TRANS_LEN 1 268c2ecf20Sopenharmony_ci#define SLIC_TWO_LEN 2 278c2ecf20Sopenharmony_ci#define SLIC_THREE_LEN 3 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic struct spi_device *g_spi; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 328c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zhao Qiang<B45475@freescale.com>"); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* the read/write format of address is 358c2ecf20Sopenharmony_ci * w/r|A13|A12|A11|A10|A9|A8|A7|A6|A5|A4|A3|A2|A1|A0|x 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_cistatic void slic_write(struct spi_device *spi, u16 addr, 388c2ecf20Sopenharmony_ci u8 data) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci u8 temp[3]; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci addr = bitrev16(addr) >> 1; 438c2ecf20Sopenharmony_ci data = bitrev8(data); 448c2ecf20Sopenharmony_ci temp[0] = (u8)((addr >> 8) & 0x7f); 458c2ecf20Sopenharmony_ci temp[1] = (u8)(addr & 0xfe); 468c2ecf20Sopenharmony_ci temp[2] = data; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci /* write spi addr and value */ 498c2ecf20Sopenharmony_ci spi_write(spi, &temp[0], SLIC_THREE_LEN); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic u8 slic_read(struct spi_device *spi, u16 addr) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci u8 temp[2]; 558c2ecf20Sopenharmony_ci u8 data; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci addr = bitrev16(addr) >> 1; 588c2ecf20Sopenharmony_ci temp[0] = (u8)(((addr >> 8) & 0x7f) | 0x80); 598c2ecf20Sopenharmony_ci temp[1] = (u8)(addr & 0xfe); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci spi_write_then_read(spi, &temp[0], SLIC_TWO_LEN, &data, 628c2ecf20Sopenharmony_ci SLIC_TRANS_LEN); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci data = bitrev8(data); 658c2ecf20Sopenharmony_ci return data; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic bool get_slic_product_code(struct spi_device *spi) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci u8 device_id; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci device_id = slic_read(spi, DS26522_IDR_ADDR); 738c2ecf20Sopenharmony_ci if ((device_id & 0xf8) == 0x68) 748c2ecf20Sopenharmony_ci return true; 758c2ecf20Sopenharmony_ci else 768c2ecf20Sopenharmony_ci return false; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic void ds26522_e1_spec_config(struct spi_device *spi) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci /* Receive E1 Mode, Framer Disabled */ 828c2ecf20Sopenharmony_ci slic_write(spi, DS26522_RMMR_ADDR, DS26522_RMMR_E1); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Transmit E1 Mode, Framer Disable */ 858c2ecf20Sopenharmony_ci slic_write(spi, DS26522_TMMR_ADDR, DS26522_TMMR_E1); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* Receive E1 Mode Framer Enable */ 888c2ecf20Sopenharmony_ci slic_write(spi, DS26522_RMMR_ADDR, 898c2ecf20Sopenharmony_ci slic_read(spi, DS26522_RMMR_ADDR) | DS26522_RMMR_FRM_EN); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* Transmit E1 Mode Framer Enable */ 928c2ecf20Sopenharmony_ci slic_write(spi, DS26522_TMMR_ADDR, 938c2ecf20Sopenharmony_ci slic_read(spi, DS26522_TMMR_ADDR) | DS26522_TMMR_FRM_EN); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci /* RCR1, receive E1 B8zs & ESF */ 968c2ecf20Sopenharmony_ci slic_write(spi, DS26522_RCR1_ADDR, 978c2ecf20Sopenharmony_ci DS26522_RCR1_E1_HDB3 | DS26522_RCR1_E1_CCS); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* RSYSCLK=2.048MHz, RSYNC-Output */ 1008c2ecf20Sopenharmony_ci slic_write(spi, DS26522_RIOCR_ADDR, 1018c2ecf20Sopenharmony_ci DS26522_RIOCR_2048KHZ | DS26522_RIOCR_RSIO_OUT); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* TCR1 Transmit E1 b8zs */ 1048c2ecf20Sopenharmony_ci slic_write(spi, DS26522_TCR1_ADDR, DS26522_TCR1_TB8ZS); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* TSYSCLK=2.048MHz, TSYNC-Output */ 1078c2ecf20Sopenharmony_ci slic_write(spi, DS26522_TIOCR_ADDR, 1088c2ecf20Sopenharmony_ci DS26522_TIOCR_2048KHZ | DS26522_TIOCR_TSIO_OUT); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* Set E1TAF */ 1118c2ecf20Sopenharmony_ci slic_write(spi, DS26522_E1TAF_ADDR, DS26522_E1TAF_DEFAULT); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci /* Set E1TNAF register */ 1148c2ecf20Sopenharmony_ci slic_write(spi, DS26522_E1TNAF_ADDR, DS26522_E1TNAF_DEFAULT); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci /* Receive E1 Mode Framer Enable & init Done */ 1178c2ecf20Sopenharmony_ci slic_write(spi, DS26522_RMMR_ADDR, slic_read(spi, DS26522_RMMR_ADDR) | 1188c2ecf20Sopenharmony_ci DS26522_RMMR_INIT_DONE); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* Transmit E1 Mode Framer Enable & init Done */ 1218c2ecf20Sopenharmony_ci slic_write(spi, DS26522_TMMR_ADDR, slic_read(spi, DS26522_TMMR_ADDR) | 1228c2ecf20Sopenharmony_ci DS26522_TMMR_INIT_DONE); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci /* Configure LIU E1 mode */ 1258c2ecf20Sopenharmony_ci slic_write(spi, DS26522_LTRCR_ADDR, DS26522_LTRCR_E1); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* E1 Mode default 75 ohm w/Transmit Impedance Matlinking */ 1288c2ecf20Sopenharmony_ci slic_write(spi, DS26522_LTITSR_ADDR, 1298c2ecf20Sopenharmony_ci DS26522_LTITSR_TLIS_75OHM | DS26522_LTITSR_LBOS_75OHM); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci /* E1 Mode default 75 ohm Long Haul w/Receive Impedance Matlinking */ 1328c2ecf20Sopenharmony_ci slic_write(spi, DS26522_LRISMR_ADDR, 1338c2ecf20Sopenharmony_ci DS26522_LRISMR_75OHM | DS26522_LRISMR_MAX); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* Enable Transmit output */ 1368c2ecf20Sopenharmony_ci slic_write(spi, DS26522_LMCR_ADDR, DS26522_LMCR_TE); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int slic_ds26522_init_configure(struct spi_device *spi) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci u16 addr; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci /* set clock */ 1448c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GTCCR_ADDR, DS26522_GTCCR_BPREFSEL_REFCLKIN | 1458c2ecf20Sopenharmony_ci DS26522_GTCCR_BFREQSEL_2048KHZ | 1468c2ecf20Sopenharmony_ci DS26522_GTCCR_FREQSEL_2048KHZ); 1478c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GTCR2_ADDR, DS26522_GTCR2_TSSYNCOUT); 1488c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GFCR_ADDR, DS26522_GFCR_BPCLK_2048KHZ); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci /* set gtcr */ 1518c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GTCR1_ADDR, DS26522_GTCR1); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* Global LIU Software Reset Register */ 1548c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GLSRR_ADDR, DS26522_GLSRR_RESET); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* Global Framer and BERT Software Reset Register */ 1578c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GFSRR_ADDR, DS26522_GFSRR_RESET); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci usleep_range(100, 120); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GLSRR_ADDR, DS26522_GLSRR_NORMAL); 1628c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GFSRR_ADDR, DS26522_GFSRR_NORMAL); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* Perform RX/TX SRESET,Reset receiver */ 1658c2ecf20Sopenharmony_ci slic_write(spi, DS26522_RMMR_ADDR, DS26522_RMMR_SFTRST); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci /* Reset tranceiver */ 1688c2ecf20Sopenharmony_ci slic_write(spi, DS26522_TMMR_ADDR, DS26522_TMMR_SFTRST); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci usleep_range(100, 120); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* Zero all Framer Registers */ 1738c2ecf20Sopenharmony_ci for (addr = DS26522_RF_ADDR_START; addr <= DS26522_RF_ADDR_END; 1748c2ecf20Sopenharmony_ci addr++) 1758c2ecf20Sopenharmony_ci slic_write(spi, addr, 0); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci for (addr = DS26522_TF_ADDR_START; addr <= DS26522_TF_ADDR_END; 1788c2ecf20Sopenharmony_ci addr++) 1798c2ecf20Sopenharmony_ci slic_write(spi, addr, 0); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci for (addr = DS26522_LIU_ADDR_START; addr <= DS26522_LIU_ADDR_END; 1828c2ecf20Sopenharmony_ci addr++) 1838c2ecf20Sopenharmony_ci slic_write(spi, addr, 0); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci for (addr = DS26522_BERT_ADDR_START; addr <= DS26522_BERT_ADDR_END; 1868c2ecf20Sopenharmony_ci addr++) 1878c2ecf20Sopenharmony_ci slic_write(spi, addr, 0); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci /* setup ds26522 for E1 specification */ 1908c2ecf20Sopenharmony_ci ds26522_e1_spec_config(spi); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci slic_write(spi, DS26522_GTCR1_ADDR, 0x00); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci return 0; 1958c2ecf20Sopenharmony_ci} 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_cistatic int slic_ds26522_remove(struct spi_device *spi) 1988c2ecf20Sopenharmony_ci{ 1998c2ecf20Sopenharmony_ci pr_info("DS26522 module uninstalled\n"); 2008c2ecf20Sopenharmony_ci return 0; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic int slic_ds26522_probe(struct spi_device *spi) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci int ret = 0; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci g_spi = spi; 2088c2ecf20Sopenharmony_ci spi->bits_per_word = 8; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci if (!get_slic_product_code(spi)) 2118c2ecf20Sopenharmony_ci return ret; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci ret = slic_ds26522_init_configure(spi); 2148c2ecf20Sopenharmony_ci if (ret == 0) 2158c2ecf20Sopenharmony_ci pr_info("DS26522 cs%d configured\n", spi->chip_select); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return ret; 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic const struct spi_device_id slic_ds26522_id[] = { 2218c2ecf20Sopenharmony_ci { .name = "ds26522" }, 2228c2ecf20Sopenharmony_ci { /* sentinel */ }, 2238c2ecf20Sopenharmony_ci}; 2248c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, slic_ds26522_id); 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic const struct of_device_id slic_ds26522_match[] = { 2278c2ecf20Sopenharmony_ci { 2288c2ecf20Sopenharmony_ci .compatible = "maxim,ds26522", 2298c2ecf20Sopenharmony_ci }, 2308c2ecf20Sopenharmony_ci {}, 2318c2ecf20Sopenharmony_ci}; 2328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, slic_ds26522_match); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic struct spi_driver slic_ds26522_driver = { 2358c2ecf20Sopenharmony_ci .driver = { 2368c2ecf20Sopenharmony_ci .name = "ds26522", 2378c2ecf20Sopenharmony_ci .bus = &spi_bus_type, 2388c2ecf20Sopenharmony_ci .of_match_table = slic_ds26522_match, 2398c2ecf20Sopenharmony_ci }, 2408c2ecf20Sopenharmony_ci .probe = slic_ds26522_probe, 2418c2ecf20Sopenharmony_ci .remove = slic_ds26522_remove, 2428c2ecf20Sopenharmony_ci .id_table = slic_ds26522_id, 2438c2ecf20Sopenharmony_ci}; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cimodule_spi_driver(slic_ds26522_driver); 246