Path: blob/master/drivers/base/regmap/regmap-spi-avmm.c
26427 views
// SPDX-License-Identifier: GPL-2.01//2// Register map access API - SPI AVMM support3//4// Copyright (C) 2018-2020 Intel Corporation. All rights reserved.56#include <linux/module.h>7#include <linux/regmap.h>8#include <linux/spi/spi.h>9#include <linux/swab.h>1011/*12* This driver implements the regmap operations for a generic SPI13* master to access the registers of the spi slave chip which has an14* Avalone bus in it.15*16* The "SPI slave to Avalon Master Bridge" (spi-avmm) IP should be integrated17* in the spi slave chip. The IP acts as a bridge to convert encoded streams of18* bytes from the host to the internal register read/write on Avalon bus. In19* order to issue register access requests to the slave chip, the host should20* send formatted bytes that conform to the transfer protocol.21* The transfer protocol contains 3 layers: transaction layer, packet layer22* and physical layer.23*24* Reference Documents could be found at:25* https://www.intel.com/content/www/us/en/programmable/documentation/sfo1400787952932.html26*27* Chapter "SPI Slave/JTAG to Avalon Master Bridge Cores" is a general28* introduction to the protocol.29*30* Chapter "Avalon Packets to Transactions Converter Core" describes31* the transaction layer.32*33* Chapter "Avalon-ST Bytes to Packets and Packets to Bytes Converter Cores"34* describes the packet layer.35*36* Chapter "Avalon-ST Serial Peripheral Interface Core" describes the37* physical layer.38*39*40* When host issues a regmap read/write, the driver will transform the request41* to byte stream layer by layer. It formats the register addr, value and42* length to the transaction layer request, then converts the request to packet43* layer bytes stream and then to physical layer bytes stream. Finally the44* driver sends the formatted byte stream over SPI bus to the slave chip.45*46* The spi-avmm IP on the slave chip decodes the byte stream and initiates47* register read/write on its internal Avalon bus, and then encodes the48* response to byte stream and sends back to host.49*50* The driver receives the byte stream, reverses the 3 layers transformation,51* and finally gets the response value (read out data for register read,52* successful written size for register write).53*/5455#define PKT_SOP 0x7a56#define PKT_EOP 0x7b57#define PKT_CHANNEL 0x7c58#define PKT_ESC 0x7d5960#define PHY_IDLE 0x4a61#define PHY_ESC 0x4d6263#define TRANS_CODE_WRITE 0x064#define TRANS_CODE_SEQ_WRITE 0x465#define TRANS_CODE_READ 0x1066#define TRANS_CODE_SEQ_READ 0x1467#define TRANS_CODE_NO_TRANS 0x7f6869#define SPI_AVMM_XFER_TIMEOUT (msecs_to_jiffies(200))7071/* slave's register addr is 32 bits */72#define SPI_AVMM_REG_SIZE 4UL73/* slave's register value is 32 bits */74#define SPI_AVMM_VAL_SIZE 4UL7576/*77* max rx size could be larger. But considering the buffer consuming,78* it is proper that we limit 1KB xfer at max.79*/80#define MAX_READ_CNT 256UL81#define MAX_WRITE_CNT 1UL8283struct trans_req_header {84u8 code;85u8 rsvd;86__be16 size;87__be32 addr;88} __packed;8990struct trans_resp_header {91u8 r_code;92u8 rsvd;93__be16 size;94} __packed;9596#define TRANS_REQ_HD_SIZE (sizeof(struct trans_req_header))97#define TRANS_RESP_HD_SIZE (sizeof(struct trans_resp_header))9899/*100* In transaction layer,101* the write request format is: Transaction request header + data102* the read request format is: Transaction request header103* the write response format is: Transaction response header104* the read response format is: pure data, no Transaction response header105*/106#define TRANS_WR_TX_SIZE(n) (TRANS_REQ_HD_SIZE + SPI_AVMM_VAL_SIZE * (n))107#define TRANS_RD_TX_SIZE TRANS_REQ_HD_SIZE108#define TRANS_TX_MAX TRANS_WR_TX_SIZE(MAX_WRITE_CNT)109110#define TRANS_RD_RX_SIZE(n) (SPI_AVMM_VAL_SIZE * (n))111#define TRANS_WR_RX_SIZE TRANS_RESP_HD_SIZE112#define TRANS_RX_MAX TRANS_RD_RX_SIZE(MAX_READ_CNT)113114/* tx & rx share one transaction layer buffer */115#define TRANS_BUF_SIZE ((TRANS_TX_MAX > TRANS_RX_MAX) ? \116TRANS_TX_MAX : TRANS_RX_MAX)117118/*119* In tx phase, the host prepares all the phy layer bytes of a request in the120* phy buffer and sends them in a batch.121*122* The packet layer and physical layer defines several special chars for123* various purpose, when a transaction layer byte hits one of these special124* chars, it should be escaped. The escape rule is, "Escape char first,125* following the byte XOR'ed with 0x20".126*127* This macro defines the max possible length of the phy data. In the worst128* case, all transaction layer bytes need to be escaped (so the data length129* doubles), plus 4 special chars (SOP, CHANNEL, CHANNEL_NUM, EOP). Finally130* we should make sure the length is aligned to SPI BPW.131*/132#define PHY_TX_MAX ALIGN(2 * TRANS_TX_MAX + 4, 4)133134/*135* Unlike tx, phy rx is affected by possible PHY_IDLE bytes from slave, the max136* length of the rx bit stream is unpredictable. So the driver reads the words137* one by one, and parses each word immediately into transaction layer buffer.138* Only one word length of phy buffer is used for rx.139*/140#define PHY_BUF_SIZE PHY_TX_MAX141142/**143* struct spi_avmm_bridge - SPI slave to AVMM bus master bridge144*145* @spi: spi slave associated with this bridge.146* @word_len: bytes of word for spi transfer.147* @trans_len: length of valid data in trans_buf.148* @phy_len: length of valid data in phy_buf.149* @trans_buf: the bridge buffer for transaction layer data.150* @phy_buf: the bridge buffer for physical layer data.151* @swap_words: the word swapping cb for phy data. NULL if not needed.152*153* As a device's registers are implemented on the AVMM bus address space, it154* requires the driver to issue formatted requests to spi slave to AVMM bus155* master bridge to perform register access.156*/157struct spi_avmm_bridge {158struct spi_device *spi;159unsigned char word_len;160unsigned int trans_len;161unsigned int phy_len;162/* bridge buffer used in translation between protocol layers */163char trans_buf[TRANS_BUF_SIZE];164char phy_buf[PHY_BUF_SIZE];165void (*swap_words)(void *buf, unsigned int len);166};167168static void br_swap_words_32(void *buf, unsigned int len)169{170swab32_array(buf, len / 4);171}172173/*174* Format transaction layer data in br->trans_buf according to the register175* access request, Store valid transaction layer data length in br->trans_len.176*/177static int br_trans_tx_prepare(struct spi_avmm_bridge *br, bool is_read, u32 reg,178u32 *wr_val, u32 count)179{180struct trans_req_header *header;181unsigned int trans_len;182u8 code;183__le32 *data;184int i;185186if (is_read) {187if (count == 1)188code = TRANS_CODE_READ;189else190code = TRANS_CODE_SEQ_READ;191} else {192if (count == 1)193code = TRANS_CODE_WRITE;194else195code = TRANS_CODE_SEQ_WRITE;196}197198header = (struct trans_req_header *)br->trans_buf;199header->code = code;200header->rsvd = 0;201header->size = cpu_to_be16((u16)count * SPI_AVMM_VAL_SIZE);202header->addr = cpu_to_be32(reg);203204trans_len = TRANS_REQ_HD_SIZE;205206if (!is_read) {207trans_len += SPI_AVMM_VAL_SIZE * count;208if (trans_len > sizeof(br->trans_buf))209return -ENOMEM;210211data = (__le32 *)(br->trans_buf + TRANS_REQ_HD_SIZE);212213for (i = 0; i < count; i++)214*data++ = cpu_to_le32(*wr_val++);215}216217/* Store valid trans data length for next layer */218br->trans_len = trans_len;219220return 0;221}222223/*224* Convert transaction layer data (in br->trans_buf) to phy layer data, store225* them in br->phy_buf. Pad the phy_buf aligned with SPI's BPW. Store valid phy226* layer data length in br->phy_len.227*228* phy_buf len should be aligned with SPI's BPW. Spare bytes should be padded229* with PHY_IDLE, then the slave will just drop them.230*231* The driver will not simply pad 4a at the tail. The concern is that driver232* will not store MISO data during tx phase, if the driver pads 4a at the tail,233* it is possible that if the slave is fast enough to response at the padding234* time. As a result these rx bytes are lost. In the following case, 7a,7c,00235* will lost.236* MOSI ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|4a|4a|4a| |XX|XX|...237* MISO ...|4a|4a|4a|4a| |4a|4a|4a|4a| |4a|4a|4a|4a| |4a|7a|7c|00| |78|56|...238*239* So the driver moves EOP and bytes after EOP to the end of the aligned size,240* then fill the hole with PHY_IDLE. As following:241* before pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|242* after pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|4a| |4a|4a|7b|40|243* Then if the slave will not get the entire packet before the tx phase is244* over, it can't responsed to anything either.245*/246static int br_pkt_phy_tx_prepare(struct spi_avmm_bridge *br)247{248char *tb, *tb_end, *pb, *pb_limit, *pb_eop = NULL;249unsigned int aligned_phy_len, move_size;250bool need_esc = false;251252tb = br->trans_buf;253tb_end = tb + br->trans_len;254pb = br->phy_buf;255pb_limit = pb + ARRAY_SIZE(br->phy_buf);256257*pb++ = PKT_SOP;258259/*260* The driver doesn't support multiple channels so the channel number261* is always 0.262*/263*pb++ = PKT_CHANNEL;264*pb++ = 0x0;265266for (; pb < pb_limit && tb < tb_end; pb++) {267if (need_esc) {268*pb = *tb++ ^ 0x20;269need_esc = false;270continue;271}272273/* EOP should be inserted before the last valid char */274if (tb == tb_end - 1 && !pb_eop) {275*pb = PKT_EOP;276pb_eop = pb;277continue;278}279280/*281* insert an ESCAPE char if the data value equals any special282* char.283*/284switch (*tb) {285case PKT_SOP:286case PKT_EOP:287case PKT_CHANNEL:288case PKT_ESC:289*pb = PKT_ESC;290need_esc = true;291break;292case PHY_IDLE:293case PHY_ESC:294*pb = PHY_ESC;295need_esc = true;296break;297default:298*pb = *tb++;299break;300}301}302303/* The phy buffer is used out but transaction layer data remains */304if (tb < tb_end)305return -ENOMEM;306307/* Store valid phy data length for spi transfer */308br->phy_len = pb - br->phy_buf;309310if (br->word_len == 1)311return 0;312313/* Do phy buf padding if word_len > 1 byte. */314aligned_phy_len = ALIGN(br->phy_len, br->word_len);315if (aligned_phy_len > sizeof(br->phy_buf))316return -ENOMEM;317318if (aligned_phy_len == br->phy_len)319return 0;320321/* move EOP and bytes after EOP to the end of aligned size */322move_size = pb - pb_eop;323memmove(&br->phy_buf[aligned_phy_len - move_size], pb_eop, move_size);324325/* fill the hole with PHY_IDLEs */326memset(pb_eop, PHY_IDLE, aligned_phy_len - br->phy_len);327328/* update the phy data length */329br->phy_len = aligned_phy_len;330331return 0;332}333334/*335* In tx phase, the slave only returns PHY_IDLE (0x4a). So the driver will336* ignore rx in tx phase.337*/338static int br_do_tx(struct spi_avmm_bridge *br)339{340/* reorder words for spi transfer */341if (br->swap_words)342br->swap_words(br->phy_buf, br->phy_len);343344/* send all data in phy_buf */345return spi_write(br->spi, br->phy_buf, br->phy_len);346}347348/*349* This function read the rx byte stream from SPI word by word and convert350* them to transaction layer data in br->trans_buf. It also stores the length351* of rx transaction layer data in br->trans_len352*353* The slave may send an unknown number of PHY_IDLEs in rx phase, so we cannot354* prepare a fixed length buffer to receive all of the rx data in a batch. We355* have to read word by word and convert them to transaction layer data at356* once.357*/358static int br_do_rx_and_pkt_phy_parse(struct spi_avmm_bridge *br)359{360bool eop_found = false, channel_found = false, esc_found = false;361bool valid_word = false, last_try = false;362struct device *dev = &br->spi->dev;363char *pb, *tb_limit, *tb = NULL;364unsigned long poll_timeout;365int ret, i;366367tb_limit = br->trans_buf + ARRAY_SIZE(br->trans_buf);368pb = br->phy_buf;369poll_timeout = jiffies + SPI_AVMM_XFER_TIMEOUT;370while (tb < tb_limit) {371ret = spi_read(br->spi, pb, br->word_len);372if (ret)373return ret;374375/* reorder the word back */376if (br->swap_words)377br->swap_words(pb, br->word_len);378379valid_word = false;380for (i = 0; i < br->word_len; i++) {381/* drop everything before first SOP */382if (!tb && pb[i] != PKT_SOP)383continue;384385/* drop PHY_IDLE */386if (pb[i] == PHY_IDLE)387continue;388389valid_word = true;390391/*392* We don't support multiple channels, so error out if393* a non-zero channel number is found.394*/395if (channel_found) {396if (pb[i] != 0) {397dev_err(dev, "%s channel num != 0\n",398__func__);399return -EFAULT;400}401402channel_found = false;403continue;404}405406switch (pb[i]) {407case PKT_SOP:408/*409* reset the parsing if a second SOP appears.410*/411tb = br->trans_buf;412eop_found = false;413channel_found = false;414esc_found = false;415break;416case PKT_EOP:417/*418* No special char is expected after ESC char.419* No special char (except ESC & PHY_IDLE) is420* expected after EOP char.421*422* The special chars are all dropped.423*/424if (esc_found || eop_found)425return -EFAULT;426427eop_found = true;428break;429case PKT_CHANNEL:430if (esc_found || eop_found)431return -EFAULT;432433channel_found = true;434break;435case PKT_ESC:436case PHY_ESC:437if (esc_found)438return -EFAULT;439440esc_found = true;441break;442default:443/* Record the normal byte in trans_buf. */444if (esc_found) {445*tb++ = pb[i] ^ 0x20;446esc_found = false;447} else {448*tb++ = pb[i];449}450451/*452* We get the last normal byte after EOP, it is453* time we finish. Normally the function should454* return here.455*/456if (eop_found) {457br->trans_len = tb - br->trans_buf;458return 0;459}460}461}462463if (valid_word) {464/* update poll timeout when we get valid word */465poll_timeout = jiffies + SPI_AVMM_XFER_TIMEOUT;466last_try = false;467} else {468/*469* We timeout when rx keeps invalid for some time. But470* it is possible we are scheduled out for long time471* after a spi_read. So when we are scheduled in, a SW472* timeout happens. But actually HW may have worked fine and473* has been ready long time ago. So we need to do an extra474* read, if we get a valid word then we could continue rx,475* otherwise real a HW issue happens.476*/477if (last_try)478return -ETIMEDOUT;479480if (time_after(jiffies, poll_timeout))481last_try = true;482}483}484485/*486* We have used out all transfer layer buffer but cannot find the end487* of the byte stream.488*/489dev_err(dev, "%s transfer buffer is full but rx doesn't end\n",490__func__);491492return -EFAULT;493}494495/*496* For read transactions, the avmm bus will directly return register values497* without transaction response header.498*/499static int br_rd_trans_rx_parse(struct spi_avmm_bridge *br,500u32 *val, unsigned int expected_count)501{502unsigned int i, trans_len = br->trans_len;503__le32 *data;504505if (expected_count * SPI_AVMM_VAL_SIZE != trans_len)506return -EFAULT;507508data = (__le32 *)br->trans_buf;509for (i = 0; i < expected_count; i++)510*val++ = le32_to_cpu(*data++);511512return 0;513}514515/*516* For write transactions, the slave will return a transaction response517* header.518*/519static int br_wr_trans_rx_parse(struct spi_avmm_bridge *br,520unsigned int expected_count)521{522unsigned int trans_len = br->trans_len;523struct trans_resp_header *resp;524u8 code;525u16 val_len;526527if (trans_len != TRANS_RESP_HD_SIZE)528return -EFAULT;529530resp = (struct trans_resp_header *)br->trans_buf;531532code = resp->r_code ^ 0x80;533val_len = be16_to_cpu(resp->size);534if (!val_len || val_len != expected_count * SPI_AVMM_VAL_SIZE)535return -EFAULT;536537/* error out if the trans code doesn't align with the val size */538if ((val_len == SPI_AVMM_VAL_SIZE && code != TRANS_CODE_WRITE) ||539(val_len > SPI_AVMM_VAL_SIZE && code != TRANS_CODE_SEQ_WRITE))540return -EFAULT;541542return 0;543}544545static int do_reg_access(void *context, bool is_read, unsigned int reg,546unsigned int *value, unsigned int count)547{548struct spi_avmm_bridge *br = context;549int ret;550551/* invalidate bridge buffers first */552br->trans_len = 0;553br->phy_len = 0;554555ret = br_trans_tx_prepare(br, is_read, reg, value, count);556if (ret)557return ret;558559ret = br_pkt_phy_tx_prepare(br);560if (ret)561return ret;562563ret = br_do_tx(br);564if (ret)565return ret;566567ret = br_do_rx_and_pkt_phy_parse(br);568if (ret)569return ret;570571if (is_read)572return br_rd_trans_rx_parse(br, value, count);573else574return br_wr_trans_rx_parse(br, count);575}576577static int regmap_spi_avmm_gather_write(void *context,578const void *reg_buf, size_t reg_len,579const void *val_buf, size_t val_len)580{581if (reg_len != SPI_AVMM_REG_SIZE)582return -EINVAL;583584if (!IS_ALIGNED(val_len, SPI_AVMM_VAL_SIZE))585return -EINVAL;586587return do_reg_access(context, false, *(u32 *)reg_buf, (u32 *)val_buf,588val_len / SPI_AVMM_VAL_SIZE);589}590591static int regmap_spi_avmm_write(void *context, const void *data, size_t bytes)592{593if (bytes < SPI_AVMM_REG_SIZE + SPI_AVMM_VAL_SIZE)594return -EINVAL;595596return regmap_spi_avmm_gather_write(context, data, SPI_AVMM_REG_SIZE,597data + SPI_AVMM_REG_SIZE,598bytes - SPI_AVMM_REG_SIZE);599}600601static int regmap_spi_avmm_read(void *context,602const void *reg_buf, size_t reg_len,603void *val_buf, size_t val_len)604{605if (reg_len != SPI_AVMM_REG_SIZE)606return -EINVAL;607608if (!IS_ALIGNED(val_len, SPI_AVMM_VAL_SIZE))609return -EINVAL;610611return do_reg_access(context, true, *(u32 *)reg_buf, val_buf,612(val_len / SPI_AVMM_VAL_SIZE));613}614615static struct spi_avmm_bridge *616spi_avmm_bridge_ctx_gen(struct spi_device *spi)617{618struct spi_avmm_bridge *br;619620if (!spi)621return ERR_PTR(-ENODEV);622623/* Only support BPW == 8 or 32 now. Try 32 BPW first. */624spi->mode = SPI_MODE_1;625spi->bits_per_word = 32;626if (spi_setup(spi)) {627spi->bits_per_word = 8;628if (spi_setup(spi))629return ERR_PTR(-EINVAL);630}631632br = kzalloc(sizeof(*br), GFP_KERNEL);633if (!br)634return ERR_PTR(-ENOMEM);635636br->spi = spi;637br->word_len = spi->bits_per_word / 8;638if (br->word_len == 4) {639/*640* The protocol requires little endian byte order but MSB641* first. So driver needs to swap the byte order word by word642* if word length > 1.643*/644br->swap_words = br_swap_words_32;645}646647return br;648}649650static void spi_avmm_bridge_ctx_free(void *context)651{652kfree(context);653}654655static const struct regmap_bus regmap_spi_avmm_bus = {656.write = regmap_spi_avmm_write,657.gather_write = regmap_spi_avmm_gather_write,658.read = regmap_spi_avmm_read,659.reg_format_endian_default = REGMAP_ENDIAN_NATIVE,660.val_format_endian_default = REGMAP_ENDIAN_NATIVE,661.max_raw_read = SPI_AVMM_VAL_SIZE * MAX_READ_CNT,662.max_raw_write = SPI_AVMM_VAL_SIZE * MAX_WRITE_CNT,663.free_context = spi_avmm_bridge_ctx_free,664};665666struct regmap *__regmap_init_spi_avmm(struct spi_device *spi,667const struct regmap_config *config,668struct lock_class_key *lock_key,669const char *lock_name)670{671struct spi_avmm_bridge *bridge;672struct regmap *map;673674bridge = spi_avmm_bridge_ctx_gen(spi);675if (IS_ERR(bridge))676return ERR_CAST(bridge);677678map = __regmap_init(&spi->dev, ®map_spi_avmm_bus,679bridge, config, lock_key, lock_name);680if (IS_ERR(map)) {681spi_avmm_bridge_ctx_free(bridge);682return ERR_CAST(map);683}684685return map;686}687EXPORT_SYMBOL_GPL(__regmap_init_spi_avmm);688689struct regmap *__devm_regmap_init_spi_avmm(struct spi_device *spi,690const struct regmap_config *config,691struct lock_class_key *lock_key,692const char *lock_name)693{694struct spi_avmm_bridge *bridge;695struct regmap *map;696697bridge = spi_avmm_bridge_ctx_gen(spi);698if (IS_ERR(bridge))699return ERR_CAST(bridge);700701map = __devm_regmap_init(&spi->dev, ®map_spi_avmm_bus,702bridge, config, lock_key, lock_name);703if (IS_ERR(map)) {704spi_avmm_bridge_ctx_free(bridge);705return ERR_CAST(map);706}707708return map;709}710EXPORT_SYMBOL_GPL(__devm_regmap_init_spi_avmm);711712MODULE_DESCRIPTION("Register map access API - SPI AVMM support");713MODULE_LICENSE("GPL v2");714715716