···381381#else382382383383static void prepare_dma(struct s3c64xx_spi_dma_data *dma,384384- unsigned len, dma_addr_t buf)384384+ struct sg_table *sgt)385385{386386 struct s3c64xx_spi_driver_data *sdd;387387 struct dma_slave_config config;···407407 dmaengine_slave_config(dma->ch, &config);408408 }409409410410- desc = dmaengine_prep_slave_single(dma->ch, buf, len,411411- dma->direction, DMA_PREP_INTERRUPT);410410+ desc = dmaengine_prep_slave_sg(dma->ch, sgt->sgl, sgt->nents,411411+ dma->direction, DMA_PREP_INTERRUPT);412412413413 desc->callback = s3c64xx_spi_dmacb;414414 desc->callback_param = dma;···515515 chcfg |= S3C64XX_SPI_CH_TXCH_ON;516516 if (dma_mode) {517517 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;518518+#ifndef CONFIG_S3C_DMA519519+ prepare_dma(&sdd->tx_dma, &xfer->tx_sg);520520+#else518521 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);522522+#endif519523 } else {520524 switch (sdd->cur_bpw) {521525 case 32:···551547 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)552548 | S3C64XX_SPI_PACKET_CNT_EN,553549 regs + S3C64XX_SPI_PACKET_CNT);550550+#ifndef CONFIG_S3C_DMA551551+ prepare_dma(&sdd->rx_dma, &xfer->rx_sg);552552+#else554553 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);554554+#endif555555 }556556 }557557558558 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);559559 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);560560-}561561-562562-static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,563563- struct spi_device *spi)564564-{565565- if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */566566- if (sdd->tgl_spi != spi) { /* if last mssg on diff device */567567- /* Deselect the last toggled device */568568- if (spi->cs_gpio >= 0)569569- gpio_set_value(spi->cs_gpio,570570- spi->mode & SPI_CS_HIGH ? 0 : 1);571571- }572572- sdd->tgl_spi = NULL;573573- }574574-575575- if (spi->cs_gpio >= 0)576576- gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH ? 1 : 0);577560}578561579562static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,···584593 return RX_FIFO_LVL(status, sdd);585594}586595587587-static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,588588- struct spi_transfer *xfer, int dma_mode)596596+static int wait_for_dma(struct s3c64xx_spi_driver_data *sdd,597597+ struct spi_transfer *xfer)589598{590599 void __iomem *regs = sdd->regs;591600 unsigned long val;601601+ u32 status;592602 int ms;593603594604 /* millisecs to xfer 'len' bytes @ 'cur_speed' */595605 ms = xfer->len * 8 * 1000 / sdd->cur_speed;596606 ms += 10; /* some tolerance */597607598598- if (dma_mode) {599599- val = msecs_to_jiffies(ms) + 10;600600- val = wait_for_completion_timeout(&sdd->xfer_completion, val);601601- } else {602602- u32 status;603603- val = msecs_to_loops(ms);604604- do {608608+ val = msecs_to_jiffies(ms) + 10;609609+ val = wait_for_completion_timeout(&sdd->xfer_completion, val);610610+611611+ /*612612+ * If the previous xfer was completed within timeout, then613613+ * proceed further else return -EIO.614614+ * DmaTx returns after simply writing data in the FIFO,615615+ * w/o waiting for real transmission on the bus to finish.616616+ * DmaRx returns only after Dma read data from FIFO which617617+ * needs bus transmission to finish, so we don't worry if618618+ * Xfer involved Rx(with or without Tx).619619+ */620620+ if (val && !xfer->rx_buf) {621621+ val = msecs_to_loops(10);622622+ status = readl(regs + S3C64XX_SPI_STATUS);623623+ while ((TX_FIFO_LVL(status, sdd)624624+ || !S3C64XX_SPI_ST_TX_DONE(status, sdd))625625+ && --val) {626626+ cpu_relax();605627 status = readl(regs + S3C64XX_SPI_STATUS);606606- } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);607607- }608608-609609- if (dma_mode) {610610- u32 status;611611-612612- /*613613- * If the previous xfer was completed within timeout, then614614- * proceed further else return -EIO.615615- * DmaTx returns after simply writing data in the FIFO,616616- * w/o waiting for real transmission on the bus to finish.617617- * DmaRx returns only after Dma read data from FIFO which618618- * needs bus transmission to finish, so we don't worry if619619- * Xfer involved Rx(with or without Tx).620620- */621621- if (val && !xfer->rx_buf) {622622- val = msecs_to_loops(10);623623- status = readl(regs + S3C64XX_SPI_STATUS);624624- while ((TX_FIFO_LVL(status, sdd)625625- || !S3C64XX_SPI_ST_TX_DONE(status, sdd))626626- && --val) {627627- cpu_relax();628628- status = readl(regs + S3C64XX_SPI_STATUS);629629- }630630-631628 }632629633633- /* If timed out while checking rx/tx status return error */634634- if (!val)635635- return -EIO;636636- } else {637637- int loops;638638- u32 cpy_len;639639- u8 *buf;640640-641641- /* If it was only Tx */642642- if (!xfer->rx_buf) {643643- sdd->state &= ~TXBUSY;644644- return 0;645645- }646646-647647- /*648648- * If the receive length is bigger than the controller fifo649649- * size, calculate the loops and read the fifo as many times.650650- * loops = length / max fifo size (calculated by using the651651- * fifo mask).652652- * For any size less than the fifo size the below code is653653- * executed atleast once.654654- */655655- loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);656656- buf = xfer->rx_buf;657657- do {658658- /* wait for data to be received in the fifo */659659- cpy_len = s3c64xx_spi_wait_for_timeout(sdd,660660- (loops ? ms : 0));661661-662662- switch (sdd->cur_bpw) {663663- case 32:664664- ioread32_rep(regs + S3C64XX_SPI_RX_DATA,665665- buf, cpy_len / 4);666666- break;667667- case 16:668668- ioread16_rep(regs + S3C64XX_SPI_RX_DATA,669669- buf, cpy_len / 2);670670- break;671671- default:672672- ioread8_rep(regs + S3C64XX_SPI_RX_DATA,673673- buf, cpy_len);674674- break;675675- }676676-677677- buf = buf + cpy_len;678678- } while (loops--);679679- sdd->state &= ~RXBUSY;680630 }631631+632632+ /* If timed out while checking rx/tx status return error */633633+ if (!val)634634+ return -EIO;681635682636 return 0;683637}684638685685-static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,686686- struct spi_device *spi)639639+static int wait_for_pio(struct s3c64xx_spi_driver_data *sdd,640640+ struct spi_transfer *xfer)687641{688688- if (sdd->tgl_spi == spi)689689- sdd->tgl_spi = NULL;642642+ void __iomem *regs = sdd->regs;643643+ unsigned long val;644644+ u32 status;645645+ int loops;646646+ u32 cpy_len;647647+ u8 *buf;648648+ int ms;690649691691- if (spi->cs_gpio >= 0)692692- gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);650650+ /* millisecs to xfer 'len' bytes @ 'cur_speed' */651651+ ms = xfer->len * 8 * 1000 / sdd->cur_speed;652652+ ms += 10; /* some tolerance */653653+654654+ val = msecs_to_loops(ms);655655+ do {656656+ status = readl(regs + S3C64XX_SPI_STATUS);657657+ } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);658658+659659+660660+ /* If it was only Tx */661661+ if (!xfer->rx_buf) {662662+ sdd->state &= ~TXBUSY;663663+ return 0;664664+ }665665+666666+ /*667667+ * If the receive length is bigger than the controller fifo668668+ * size, calculate the loops and read the fifo as many times.669669+ * loops = length / max fifo size (calculated by using the670670+ * fifo mask).671671+ * For any size less than the fifo size the below code is672672+ * executed atleast once.673673+ */674674+ loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);675675+ buf = xfer->rx_buf;676676+ do {677677+ /* wait for data to be received in the fifo */678678+ cpy_len = s3c64xx_spi_wait_for_timeout(sdd,679679+ (loops ? ms : 0));680680+681681+ switch (sdd->cur_bpw) {682682+ case 32:683683+ ioread32_rep(regs + S3C64XX_SPI_RX_DATA,684684+ buf, cpy_len / 4);685685+ break;686686+ case 16:687687+ ioread16_rep(regs + S3C64XX_SPI_RX_DATA,688688+ buf, cpy_len / 2);689689+ break;690690+ default:691691+ ioread8_rep(regs + S3C64XX_SPI_RX_DATA,692692+ buf, cpy_len);693693+ break;694694+ }695695+696696+ buf = buf + cpy_len;697697+ } while (loops--);698698+ sdd->state &= ~RXBUSY;699699+700700+ return 0;693701}694702695703static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)···919929920930 spin_unlock_irqrestore(&sdd->lock, flags);921931922922- status = wait_for_xfer(sdd, xfer, use_dma);932932+ if (use_dma)933933+ status = wait_for_dma(sdd, xfer);934934+ else935935+ status = wait_for_pio(sdd, xfer);923936924937 if (status) {925938 dev_err(&spi->dev, "I/O Error: rx-%d tx-%d res:rx-%c tx-%c len-%d\n",···1085109210861093 pm_runtime_put(&sdd->pdev->dev);10871094 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);10881088- disable_cs(sdd, spi);10891095 return 0;1090109610911097setup_exit:10921098 pm_runtime_put(&sdd->pdev->dev);10931099 /* setup() returns with device de-selected */10941100 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);10951095- disable_cs(sdd, spi);1096110110971102 gpio_free(cs->line);10981103 spi_set_ctldata(spi, NULL);
+180
drivers/spi/spi.c
···2424#include <linux/device.h>2525#include <linux/init.h>2626#include <linux/cache.h>2727+#include <linux/dma-mapping.h>2828+#include <linux/dmaengine.h>2729#include <linux/mutex.h>2830#include <linux/of_device.h>2931#include <linux/of_irq.h>···582580 spi->master->set_cs(spi, !enable);583581}584582583583+static int spi_map_buf(struct spi_master *master, struct device *dev,584584+ struct sg_table *sgt, void *buf, size_t len,585585+ enum dma_data_direction dir)586586+{587587+ const bool vmalloced_buf = is_vmalloc_addr(buf);588588+ const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len;589589+ const int sgs = DIV_ROUND_UP(len, desc_len);590590+ struct page *vm_page;591591+ void *sg_buf;592592+ size_t min;593593+ int i, ret;594594+595595+ ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);596596+ if (ret != 0)597597+ return ret;598598+599599+ for (i = 0; i < sgs; i++) {600600+ min = min_t(size_t, len, desc_len);601601+602602+ if (vmalloced_buf) {603603+ vm_page = vmalloc_to_page(buf);604604+ if (!vm_page) {605605+ sg_free_table(sgt);606606+ return -ENOMEM;607607+ }608608+ sg_buf = page_address(vm_page) +609609+ ((size_t)buf & ~PAGE_MASK);610610+ } else {611611+ sg_buf = buf;612612+ }613613+614614+ sg_set_buf(&sgt->sgl[i], sg_buf, min);615615+616616+ buf += min;617617+ len -= min;618618+ }619619+620620+ ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);621621+ if (ret < 0) {622622+ sg_free_table(sgt);623623+ return ret;624624+ }625625+626626+ sgt->nents = ret;627627+628628+ return 0;629629+}630630+631631+static void spi_unmap_buf(struct spi_master *master, struct device *dev,632632+ struct sg_table *sgt, enum dma_data_direction dir)633633+{634634+ if (sgt->orig_nents) {635635+ dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);636636+ sg_free_table(sgt);637637+ }638638+}639639+640640+static int spi_map_msg(struct spi_master *master, struct spi_message *msg)641641+{642642+ struct device *tx_dev, *rx_dev;643643+ struct spi_transfer *xfer;644644+ void *tmp;645645+ unsigned int max_tx, max_rx;646646+ int ret;647647+648648+ if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {649649+ max_tx = 0;650650+ max_rx = 0;651651+652652+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {653653+ if ((master->flags & SPI_MASTER_MUST_TX) &&654654+ !xfer->tx_buf)655655+ max_tx = max(xfer->len, max_tx);656656+ if ((master->flags & SPI_MASTER_MUST_RX) &&657657+ !xfer->rx_buf)658658+ max_rx = max(xfer->len, max_rx);659659+ }660660+661661+ if (max_tx) {662662+ tmp = krealloc(master->dummy_tx, max_tx,663663+ GFP_KERNEL | GFP_DMA);664664+ if (!tmp)665665+ return -ENOMEM;666666+ master->dummy_tx = tmp;667667+ memset(tmp, 0, max_tx);668668+ }669669+670670+ if (max_rx) {671671+ tmp = krealloc(master->dummy_rx, max_rx,672672+ GFP_KERNEL | GFP_DMA);673673+ if (!tmp)674674+ return -ENOMEM;675675+ master->dummy_rx = tmp;676676+ }677677+678678+ if (max_tx || max_rx) {679679+ list_for_each_entry(xfer, &msg->transfers,680680+ transfer_list) {681681+ if (!xfer->tx_buf)682682+ xfer->tx_buf = master->dummy_tx;683683+ if (!xfer->rx_buf)684684+ xfer->rx_buf = master->dummy_rx;685685+ }686686+ }687687+ }688688+689689+ if (!master->can_dma)690690+ return 0;691691+692692+ tx_dev = &master->dma_tx->dev->device;693693+ rx_dev = &master->dma_rx->dev->device;694694+695695+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {696696+ if (!master->can_dma(master, msg->spi, xfer))697697+ continue;698698+699699+ if (xfer->tx_buf != NULL) {700700+ ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,701701+ (void *)xfer->tx_buf, xfer->len,702702+ DMA_TO_DEVICE);703703+ if (ret != 0)704704+ return ret;705705+ }706706+707707+ if (xfer->rx_buf != NULL) {708708+ ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,709709+ xfer->rx_buf, xfer->len,710710+ DMA_FROM_DEVICE);711711+ if (ret != 0) {712712+ spi_unmap_buf(master, tx_dev, &xfer->tx_sg,713713+ DMA_TO_DEVICE);714714+ return ret;715715+ }716716+ }717717+ }718718+719719+ master->cur_msg_mapped = true;720720+721721+ return 0;722722+}723723+724724+static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg)725725+{726726+ struct spi_transfer *xfer;727727+ struct device *tx_dev, *rx_dev;728728+729729+ if (!master->cur_msg_mapped || !master->can_dma)730730+ return 0;731731+732732+ tx_dev = &master->dma_tx->dev->device;733733+ rx_dev = &master->dma_rx->dev->device;734734+735735+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {736736+ if (!master->can_dma(master, msg->spi, xfer))737737+ continue;738738+739739+ spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);740740+ spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);741741+ }742742+743743+ return 0;744744+}745745+585746/*586747 * spi_transfer_one_message - Default implementation of transfer_one_message()587748 *···851686 }852687 master->busy = false;853688 spin_unlock_irqrestore(&master->queue_lock, flags);689689+ kfree(master->dummy_rx);690690+ master->dummy_rx = NULL;691691+ kfree(master->dummy_tx);692692+ master->dummy_tx = NULL;854693 if (master->unprepare_transfer_hardware &&855694 master->unprepare_transfer_hardware(master))856695 dev_err(&master->dev,···919750 return;920751 }921752 master->cur_msg_prepared = true;753753+ }754754+755755+ ret = spi_map_msg(master, master->cur_msg);756756+ if (ret) {757757+ master->cur_msg->status = ret;758758+ spi_finalize_current_message(master);759759+ return;922760 }923761924762 ret = master->transfer_one_message(master, master->cur_msg);···10168401017841 queue_kthread_work(&master->kworker, &master->pump_messages);1018842 spin_unlock_irqrestore(&master->queue_lock, flags);843843+844844+ spi_unmap_msg(master, mesg);10198451020846 if (master->cur_msg_prepared && master->unprepare_message) {1021847 ret = master->unprepare_message(master, mesg);···15521374 mutex_init(&master->bus_lock_mutex);15531375 master->bus_lock_flag = 0;15541376 init_completion(&master->xfer_completion);13771377+ if (!master->max_dma_len)13781378+ master->max_dma_len = INT_MAX;1555137915561380 /* register the device, then userspace will see it.15571381 * registration fails if the bus ID is in use.
+31
include/linux/spi/spi.h
···2424#include <linux/slab.h>2525#include <linux/kthread.h>2626#include <linux/completion.h>2727+#include <linux/scatterlist.h>2828+2929+struct dma_chan;27302831/*2932 * INTERFACES between SPI master-side drivers and SPI infrastructure.···269266 * @auto_runtime_pm: the core should ensure a runtime PM reference is held270267 * while the hardware is prepared, using the parent271268 * device for the spidev269269+ * @max_dma_len: Maximum length of a DMA transfer for the device.272270 * @prepare_transfer_hardware: a message will soon arrive from the queue273271 * so the subsystem requests the driver to prepare the transfer hardware274272 * by issuing this call···349345#define SPI_MASTER_HALF_DUPLEX BIT(0) /* can't do full duplex */350346#define SPI_MASTER_NO_RX BIT(1) /* can't do buffer read */351347#define SPI_MASTER_NO_TX BIT(2) /* can't do buffer write */348348+#define SPI_MASTER_MUST_RX BIT(3) /* requires rx */349349+#define SPI_MASTER_MUST_TX BIT(4) /* requires tx */352350353351 /* lock and mutex for SPI bus locking */354352 spinlock_t bus_lock_spinlock;···393387 void (*cleanup)(struct spi_device *spi);394388395389 /*390390+ * Used to enable core support for DMA handling, if can_dma()391391+ * exists and returns true then the transfer will be mapped392392+ * prior to transfer_one() being called. The driver should393393+ * not modify or store xfer and dma_tx and dma_rx must be set394394+ * while the device is prepared.395395+ */396396+ bool (*can_dma)(struct spi_master *master,397397+ struct spi_device *spi,398398+ struct spi_transfer *xfer);399399+400400+ /*396401 * These hooks are for drivers that want to use the generic397402 * master transfer queueing mechanism. If these are used, the398403 * transfer() function above must NOT be specified by the driver.···421404 bool rt;422405 bool auto_runtime_pm;423406 bool cur_msg_prepared;407407+ bool cur_msg_mapped;424408 struct completion xfer_completion;409409+ size_t max_dma_len;425410426411 int (*prepare_transfer_hardware)(struct spi_master *master);427412 int (*transfer_one_message)(struct spi_master *master,···444425445426 /* gpio chip select */446427 int *cs_gpios;428428+429429+ /* DMA channels for use with core dmaengine helpers */430430+ struct dma_chan *dma_tx;431431+ struct dma_chan *dma_rx;432432+433433+ /* dummy data for full duplex devices */434434+ void *dummy_rx;435435+ void *dummy_tx;447436};448437449438static inline void *spi_master_get_devdata(struct spi_master *master)···536509 * (optionally) changing the chipselect status, then starting537510 * the next transfer or completing this @spi_message.538511 * @transfer_list: transfers are sequenced through @spi_message.transfers512512+ * @tx_sg: Scatterlist for transmit, currently not for client use513513+ * @rx_sg: Scatterlist for receive, currently not for client use539514 *540515 * SPI transfers always write the same number of bytes as they read.541516 * Protocol drivers should always provide @rx_buf and/or @tx_buf.···605576606577 dma_addr_t tx_dma;607578 dma_addr_t rx_dma;579579+ struct sg_table tx_sg;580580+ struct sg_table rx_sg;608581609582 unsigned cs_change:1;610583 unsigned tx_nbits:3;