static int terminate_request(struct cros_ec_device *ec_dev) { struct cros_ec_spi *ec_spi = ec_dev->priv; struct spi_message msg; struct spi_transfer trans; int ret; /* * Turn off CS, possibly adding a delay to ensure the rising edge * doesn't come too soon after the end of the data. */ spi_message_init(&msg); memset(&trans, 0, sizeof(trans)); trans.delay_usecs = ec_spi->end_of_msg_delay; spi_message_add_tail(&trans, &msg); ret = spi_sync_locked(ec_spi->spi, &msg); /* Reset end-of-response timer */ ec_spi->last_transfer_ns = ktime_get_ns(); if (ret < 0) { dev_err(ec_dev->dev, "cs-deassert spi transfer failed: %d\n", ret); } return ret; }
static int mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len) { int status; if (len > sizeof(*host->data)) { WARN_ON(1); return -EIO; } host->status.len = len; if (host->dma_dev) dma_sync_single_for_device(host->dma_dev, host->data_dma, sizeof(*host->data), DMA_FROM_DEVICE); status = spi_sync_locked(host->spi, &host->readback); if (host->dma_dev) dma_sync_single_for_cpu(host->dma_dev, host->data_dma, sizeof(*host->data), DMA_FROM_DEVICE); return status; }
static int __ad7192_write_reg(struct ad7192_state *st, bool locked, bool cs_change, unsigned char reg, unsigned size, unsigned val) { u8 *data = st->data; struct spi_transfer t = { .tx_buf = data, .len = size + 1, .cs_change = cs_change, }; struct spi_message m; data[0] = AD7192_COMM_WRITE | AD7192_COMM_ADDR(reg); switch (size) { case 3: data[1] = val >> 16; data[2] = val >> 8; data[3] = val; break; case 2: data[1] = val >> 8; data[2] = val; break; case 1: data[1] = val; break; default: return -EINVAL; } spi_message_init(&m); spi_message_add_tail(&t, &m); if (locked) return spi_sync_locked(st->spi, &m); else return spi_sync(st->spi, &m); } static int ad7192_write_reg(struct ad7192_state *st, unsigned reg, unsigned size, unsigned val) { return __ad7192_write_reg(st, false, false, reg, size, val); } static int __ad7192_read_reg(struct ad7192_state *st, bool locked, bool cs_change, unsigned char reg, int *val, unsigned size) { u8 *data = st->data; int ret; struct spi_transfer t[] = { { .tx_buf = data, .len = 1, }, { .rx_buf = data,
/** * ad_sd_write_reg() - Write a register * * @sigma_delta: The sigma delta device * @reg: Address of the register * @size: Size of the register (0-3) * @val: Value to write to the register * * Returns 0 on success, an error code otherwise. **/ int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg, unsigned int size, unsigned int val) { uint8_t *data = sigma_delta->data; struct spi_transfer t = { .tx_buf = data, .len = size + 1, .cs_change = sigma_delta->bus_locked, }; struct spi_message m; int ret; data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm; switch (size) { case 3: data[1] = val >> 16; data[2] = val >> 8; data[3] = val; break; case 2: put_unaligned_be16(val, &data[1]); break; case 1: data[1] = val; break; case 0: break; default: return -EINVAL; } spi_message_init(&m); spi_message_add_tail(&t, &m); if (sigma_delta->bus_locked) ret = spi_sync_locked(sigma_delta->spi, &m); else ret = spi_sync(sigma_delta->spi, &m); return ret; } EXPORT_SYMBOL_GPL(ad_sd_write_reg); static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta, unsigned int reg, unsigned int size, uint8_t *val) { uint8_t *data = sigma_delta->data; int ret; struct spi_transfer t[] = { { .tx_buf = data, .len = 1, }, { .rx_buf = val,
/** * receive_n_bytes - receive n bytes from the EC. * * Assumes buf is a pointer into the ec_dev->din buffer */ static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) { struct cros_ec_spi *ec_spi = ec_dev->priv; struct spi_transfer trans; struct spi_message msg; int ret; BUG_ON(buf - ec_dev->din + n > ec_dev->din_size); memset(&trans, 0, sizeof(trans)); trans.cs_change = 1; trans.rx_buf = buf; trans.len = n; spi_message_init(&msg); spi_message_add_tail(&trans, &msg); ret = spi_sync_locked(ec_spi->spi, &msg); if (ret < 0) dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); return ret; }
/** * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply * * @ec_dev: ChromeOS EC device * @ec_msg: Message to transfer */ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, struct cros_ec_command *ec_msg) { struct cros_ec_spi *ec_spi = ec_dev->priv; struct spi_transfer trans; struct spi_message msg; int i, len; u8 *ptr; u8 *rx_buf; int sum; int ret = 0, final_ret; len = cros_ec_prepare_tx(ec_dev, ec_msg); dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); /* If it's too soon to do another transaction, wait */ if (ec_spi->last_transfer_ns) { unsigned long delay; /* The delay completed so far */ delay = ktime_get_ns() - ec_spi->last_transfer_ns; if (delay < EC_SPI_RECOVERY_TIME_NS) ndelay(EC_SPI_RECOVERY_TIME_NS - delay); } rx_buf = kzalloc(len, GFP_KERNEL); if (!rx_buf) return -ENOMEM; spi_bus_lock(ec_spi->spi->master); /* Transmit phase - send our message */ debug_packet(ec_dev->dev, "out", ec_dev->dout, len); memset(&trans, 0, sizeof(trans)); trans.tx_buf = ec_dev->dout; trans.rx_buf = rx_buf; trans.len = len; trans.cs_change = 1; spi_message_init(&msg); spi_message_add_tail(&trans, &msg); ret = spi_sync_locked(ec_spi->spi, &msg); /* Get the response */ if (!ret) { /* Verify that EC can process command */ for (i = 0; i < len; i++) { switch (rx_buf[i]) { case EC_SPI_PAST_END: case EC_SPI_RX_BAD_DATA: case EC_SPI_NOT_READY: ret = -EAGAIN; ec_msg->result = EC_RES_IN_PROGRESS; default: break; } if (ret) break; } if (!ret) ret = cros_ec_spi_receive_response(ec_dev, ec_msg->insize + EC_MSG_TX_PROTO_BYTES); } else { dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); } final_ret = terminate_request(ec_dev); spi_bus_unlock(ec_spi->spi->master); if (!ret) ret = final_ret; if (ret < 0) goto exit; ptr = ec_dev->din; /* check response error code */ ec_msg->result = ptr[0]; ret = cros_ec_check_result(ec_dev, ec_msg); if (ret) goto exit; len = ptr[1]; sum = ptr[0] + ptr[1]; if (len > ec_msg->insize) { dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", len, ec_msg->insize); ret = -ENOSPC; goto exit; } /* copy response packet payload and compute checksum */ for (i = 0; i < len; i++) { sum += ptr[i + 2]; if (ec_msg->insize) ec_msg->data[i] = ptr[i + 2]; } sum &= 0xff; debug_packet(ec_dev->dev, "in", ptr, len + 3); if (sum != ptr[len + 2]) { dev_err(ec_dev->dev, "bad packet checksum, expected %02x, got %02x\n", sum, ptr[len + 2]); ret = -EBADMSG; goto exit; } ret = len; exit: kfree(rx_buf); if (ec_msg->command == EC_CMD_REBOOT_EC) msleep(EC_REBOOT_DELAY_MS); return ret; }
/** * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply * * @ec_dev: ChromeOS EC device * @ec_msg: Message to transfer */ static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, struct cros_ec_command *ec_msg) { struct ec_host_response *response; struct cros_ec_spi *ec_spi = ec_dev->priv; struct spi_transfer trans, trans_delay; struct spi_message msg; int i, len; u8 *ptr; u8 *rx_buf; u8 sum; int ret = 0, final_ret; len = cros_ec_prepare_tx(ec_dev, ec_msg); dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); /* If it's too soon to do another transaction, wait */ if (ec_spi->last_transfer_ns) { unsigned long delay; /* The delay completed so far */ delay = ktime_get_ns() - ec_spi->last_transfer_ns; if (delay < EC_SPI_RECOVERY_TIME_NS) ndelay(EC_SPI_RECOVERY_TIME_NS - delay); } rx_buf = kzalloc(len, GFP_KERNEL); if (!rx_buf) return -ENOMEM; spi_bus_lock(ec_spi->spi->master); /* * Leave a gap between CS assertion and clocking of data to allow the * EC time to wakeup. */ spi_message_init(&msg); if (ec_spi->start_of_msg_delay) { memset(&trans_delay, 0, sizeof(trans_delay)); trans_delay.delay_usecs = ec_spi->start_of_msg_delay; spi_message_add_tail(&trans_delay, &msg); } /* Transmit phase - send our message */ memset(&trans, 0, sizeof(trans)); trans.tx_buf = ec_dev->dout; trans.rx_buf = rx_buf; trans.len = len; trans.cs_change = 1; spi_message_add_tail(&trans, &msg); ret = spi_sync_locked(ec_spi->spi, &msg); /* Get the response */ if (!ret) { /* Verify that EC can process command */ for (i = 0; i < len; i++) { switch (rx_buf[i]) { case EC_SPI_PAST_END: case EC_SPI_RX_BAD_DATA: case EC_SPI_NOT_READY: ret = -EAGAIN; ec_msg->result = EC_RES_IN_PROGRESS; default: break; } if (ret) break; } if (!ret) ret = cros_ec_spi_receive_packet(ec_dev, ec_msg->insize + sizeof(*response)); } else { dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); } final_ret = terminate_request(ec_dev); spi_bus_unlock(ec_spi->spi->master); if (!ret) ret = final_ret; if (ret < 0) goto exit; ptr = ec_dev->din; /* check response error code */ response = (struct ec_host_response *)ptr; ec_msg->result = response->result; ret = cros_ec_check_result(ec_dev, ec_msg); if (ret) goto exit; len = response->data_len; sum = 0; if (len > ec_msg->insize) { dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", len, ec_msg->insize); ret = -EMSGSIZE; goto exit; } for (i = 0; i < sizeof(*response); i++) sum += ptr[i]; /* copy response packet payload and compute checksum */ memcpy(ec_msg->data, ptr + sizeof(*response), len); for (i = 0; i < len; i++) sum += ec_msg->data[i]; if (sum) { dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n", sum); ret = -EBADMSG; goto exit; } ret = len; exit: kfree(rx_buf); if (ec_msg->command == EC_CMD_REBOOT_EC) msleep(EC_REBOOT_DELAY_MS); return ret; }
int status; if (len > sizeof(*host->data)) { WARN_ON(1); return -EIO; } host->status.len = len; if (host->dma_dev) dma_sync_single_for_device(host->dma_dev, host->data_dma, sizeof(*host->data), DMA_FROM_DEVICE); <<<<<<< HEAD status = spi_sync_locked(host->spi, &host->readback); ======= status = spi_sync(host->spi, &host->readback); >>>>>>> 296c66da8a02d52243f45b80521febece5ed498a if (host->dma_dev) dma_sync_single_for_cpu(host->dma_dev, host->data_dma, sizeof(*host->data), DMA_FROM_DEVICE); return status; } static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout, unsigned n, u8 byte) {
static int ice40_fpga_ops_write_init(struct fpga_manager *mgr, struct fpga_image_info *info, const char *buf, size_t count) { struct ice40_fpga_priv *priv = mgr->priv; struct spi_device *dev = priv->dev; struct spi_message message; struct spi_transfer assert_cs_then_reset_delay = { .cs_change = 1, .delay_usecs = ICE40_SPI_RESET_DELAY }; struct spi_transfer housekeeping_delay_then_release_cs = { .delay_usecs = ICE40_SPI_HOUSEKEEPING_DELAY }; int ret; if ((info->flags & FPGA_MGR_PARTIAL_RECONFIG)) { dev_err(&dev->dev, "Partial reconfiguration is not supported\n"); return -ENOTSUPP; } /* Lock the bus, assert CRESET_B and SS_B and delay >200ns */ spi_bus_lock(dev->master); gpiod_set_value(priv->reset, 1); spi_message_init(&message); spi_message_add_tail(&assert_cs_then_reset_delay, &message); ret = spi_sync_locked(dev, &message); /* Come out of reset */ gpiod_set_value(priv->reset, 0); /* Abort if the chip-select failed */ if (ret) goto fail; /* Check CDONE is de-asserted i.e. the FPGA is reset */ if (gpiod_get_value(priv->cdone)) { dev_err(&dev->dev, "Device reset failed, CDONE is asserted\n"); ret = -EIO; goto fail; } /* Wait for the housekeeping to complete, and release SS_B */ spi_message_init(&message); spi_message_add_tail(&housekeeping_delay_then_release_cs, &message); ret = spi_sync_locked(dev, &message); fail: spi_bus_unlock(dev->master); return ret; } static int ice40_fpga_ops_write(struct fpga_manager *mgr, const char *buf, size_t count) { struct ice40_fpga_priv *priv = mgr->priv; return spi_write(priv->dev, buf, count); } static int ice40_fpga_ops_write_complete(struct fpga_manager *mgr, struct fpga_image_info *info) { struct ice40_fpga_priv *priv = mgr->priv; struct spi_device *dev = priv->dev; const u8 padding[ICE40_SPI_NUM_ACTIVATION_BYTES] = {0}; /* Check CDONE is asserted */ if (!gpiod_get_value(priv->cdone)) { dev_err(&dev->dev, "CDONE was not asserted after firmware transfer\n"); return -EIO; } /* Send of zero-padding to activate the firmware */ return spi_write(dev, padding, sizeof(padding)); } static const struct fpga_manager_ops ice40_fpga_ops = { .state = ice40_fpga_ops_state, .write_init = ice40_fpga_ops_write_init, .write = ice40_fpga_ops_write, .write_complete = ice40_fpga_ops_write_complete, }; static int ice40_fpga_probe(struct spi_device *spi) { struct device *dev = &spi->dev; struct ice40_fpga_priv *priv; struct fpga_manager *mgr; int ret; priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL); if (!priv) return -ENOMEM; priv->dev = spi; /* Check board setup data. */ if (spi->max_speed_hz > ICE40_SPI_MAX_SPEED) { dev_err(dev, "SPI speed is too high, maximum speed is " __stringify(ICE40_SPI_MAX_SPEED) "\n"); return -EINVAL; } if (spi->max_speed_hz < ICE40_SPI_MIN_SPEED) { dev_err(dev, "SPI speed is too low, minimum speed is " __stringify(ICE40_SPI_MIN_SPEED) "\n"); return -EINVAL; } if (spi->mode & SPI_CPHA) { dev_err(dev, "Bad SPI mode, CPHA not supported\n"); return -EINVAL; } /* Set up the GPIOs */ priv->cdone = devm_gpiod_get(dev, "cdone", GPIOD_IN); if (IS_ERR(priv->cdone)) { ret = PTR_ERR(priv->cdone); dev_err(dev, "Failed to get CDONE GPIO: %d\n", ret); return ret; } priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); if (IS_ERR(priv->reset)) { ret = PTR_ERR(priv->reset); dev_err(dev, "Failed to get CRESET_B GPIO: %d\n", ret); return ret; } mgr = fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager", &ice40_fpga_ops, priv); if (!mgr) return -ENOMEM; spi_set_drvdata(spi, mgr); ret = fpga_mgr_register(mgr); if (ret) fpga_mgr_free(mgr); return ret; } static int ice40_fpga_remove(struct spi_device *spi) { struct fpga_manager *mgr = spi_get_drvdata(spi); fpga_mgr_unregister(mgr); return 0; } static const struct of_device_id ice40_fpga_of_match[] = { { .compatible = "lattice,ice40-fpga-mgr", }, {}, };