123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116 |
- /*
- * arkmicro spi driver
- *
- * Licensed under GPLv2 or later.
- */
- #include <linux/clk.h>
- #include <linux/completion.h>
- #include <linux/delay.h>
- #include <linux/dmaengine.h>
- #include <linux/dma-mapping.h>
- #include <linux/err.h>
- #include <linux/gpio.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/irq.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- #include <linux/slab.h>
- #include <linux/spi/spi.h>
- #include <linux/spi/spi_bitbang.h>
- #include <linux/types.h>
- #include <linux/of.h>
- #include <linux/of_device.h>
- #include <linux/of_gpio.h>
- #include <linux/scatterlist.h>
- #define DRIVER_NAME "spi_ark"
- #define USE_DMA_THRESHOLD 32
- #define ARK_ECSPI_RXDATA 0x50
- #define ARK_ECSPI_TXDATA 0x460
- /* generic defines to abstract from the different register layouts */
- #define ARK_INT_RR (1 << 0) /* Receive data ready interrupt */
- #define ARK_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
- /* The maximum bytes that a sdma BD can transfer.*/
- #define MAX_SDMA_BD_BYTES (1 << 15)
- #define ARK_ECSPI_CTRL_MAX_BURST 512
- struct spi_ark_data;
- struct spi_ark_devtype_data {
- void (*intctrl)(struct spi_ark_data *, int);
- int (*config)(struct spi_device *);
- void (*trigger)(struct spi_ark_data *);
- int (*rx_available)(struct spi_ark_data *);
- void (*reset)(struct spi_ark_data *);
- bool has_dmamode;
- unsigned int fifo_size;
- };
- struct spi_ark_data {
- struct spi_bitbang bitbang;
- struct device *dev;
- struct completion xfer_done;
- void __iomem *base;
- unsigned long base_phys;
- struct clk *clk_per;
- struct clk *clk_ipg;
- unsigned long spi_clk;
- unsigned int spi_bus_clk;
- unsigned int speed_hz;
- unsigned int bits_per_word;
- unsigned int spi_drctl;
- unsigned int count, remainder;
- void (*tx)(struct spi_ark_data *);
- void (*rx)(struct spi_ark_data *);
- void *rx_buf;
- const void *tx_buf;
- unsigned int txfifo; /* number of words pushed in tx FIFO */
- unsigned int read_u32;
- unsigned int word_mask;
- bool is_arke;
- /* DMA */
- bool usedma;
- u32 wml;
- struct completion dma_rx_completion;
- struct completion dma_tx_completion;
- struct sg_table *dma_rx_sg;
- struct spi_transfer dma_transfer;
- struct spi_transfer pio_transfer;
- const struct spi_ark_devtype_data *devtype_data;
- };
- static void spi_ark_buf_rx_u8(struct spi_ark_data *spi_ark)
- {
- unsigned int val = readl(spi_ark->base + ARK_ECSPI_RXDATA);
- if (spi_ark->rx_buf) {
- if (spi_ark->is_arke)
- *(u8*)spi_ark->rx_buf = val & 0xff;
- else
- *(u8*)spi_ark->rx_buf = (val >> 24) & 0xff;
- spi_ark->rx_buf += 1;
- }
- }
- static void spi_ark_buf_rx_u16(struct spi_ark_data *spi_ark)
- {
- unsigned int val = readl(spi_ark->base + ARK_ECSPI_RXDATA);
- if (spi_ark->rx_buf) {
- if (spi_ark->is_arke)
- *(u16*)spi_ark->rx_buf = val & 0xffff;
- else
- *(u16*)spi_ark->rx_buf = (val >> 16) & 0xffff;
- spi_ark->rx_buf += 2;
- }
- }
- static void spi_ark_buf_tx_u8(struct spi_ark_data *spi_ark)
- {
- u32 val = 0;
- if (spi_ark->tx_buf) {
- if (spi_ark->is_arke)
- val = *(u8 *)spi_ark->tx_buf;
- else
- val = *(u8 *)spi_ark->tx_buf << 24;
- spi_ark->tx_buf += 1;
- }
- spi_ark->count -= 1;
- writel(val, spi_ark->base + ARK_ECSPI_TXDATA);
- }
- static void spi_ark_buf_tx_u16(struct spi_ark_data *spi_ark)
- {
- u32 val = 0;
- if (spi_ark->tx_buf) {
- if (spi_ark->is_arke)
- val = *(u16 *)spi_ark->tx_buf;
- else
- val = *(u16 *)spi_ark->tx_buf << 16;
- spi_ark->tx_buf += 2;
- }
- spi_ark->count -=2;
- writel(val, spi_ark->base + ARK_ECSPI_TXDATA);
- }
- static int spi_ark_bytes_per_word(const int bits_per_word)
- {
- return DIV_ROUND_UP(bits_per_word, BITS_PER_BYTE);
- }
- static bool spi_ark_can_dma(struct spi_master *master, struct spi_device *spi,
- struct spi_transfer *transfer)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(master);
- const u32 mszs[] = {1, 4, 8, 16};
- int idx = ARRAY_SIZE(mszs) - 1;
- struct sg_table *tx;
- struct sg_table *rx;
- struct spi_transfer *dma_xfer = &spi_ark->dma_transfer;
- struct spi_transfer *pio_xfer = &spi_ark->pio_transfer;
- int len, remainder;
- if (!master->dma_rx)
- return false;
- pio_xfer->len = 0;
- memcpy(dma_xfer, transfer, sizeof(struct spi_transfer));
- tx = &dma_xfer->tx_sg;
- rx = &dma_xfer->rx_sg;
- remainder = transfer->len & 3;
- len = transfer->len - remainder;
- if (len < USE_DMA_THRESHOLD)
- return false;
- if (remainder) {
- if (tx->nents && tx->sgl[tx->nents - 1].length > remainder &&
- rx->nents && rx->sgl[rx->nents - 1].length > remainder) {
- tx->sgl[tx->nents - 1].length -= remainder;
- rx->sgl[rx->nents - 1].length -= remainder;
- dma_xfer->len = len;
- memcpy(pio_xfer, transfer, sizeof(struct spi_transfer));
- pio_xfer->len = remainder;
- if (pio_xfer->tx_buf)
- pio_xfer->tx_buf += len;
- if (pio_xfer->rx_buf)
- pio_xfer->rx_buf += len;
- } else return false;
- }
- /* dw dma busrt should be 16,8,4,1 */
- for (; idx >= 0; idx--) {
- if (!(len % (mszs[idx] * 4)))
- break;
- }
- spi_ark->wml = mszs[idx];
- return true;
- }
- #define ARK_ECSPI_CTRL 0x08
- #define ARK_ECSPI_CTRL_ENABLE (1 << 0)
- #define ARK_ECSPI_CTRL_XCH (1 << 2)
- #define ARK_ECSPI_CTRL_SMC (1 << 3)
- #define ARK_ECSPI_CTRL_MODE_MASK (0xf << 4)
- #define ARK_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
- #define ARK_ECSPI_CTRL_POSTDIV_OFFSET 8
- #define ARK_ECSPI_CTRL_PREDIV_OFFSET 12
- #define ARK_ECSPI_CTRL_CS(cs) ((cs) << 18)
- #define ARK_ECSPI_CTRL_BL_OFFSET 20
- #define ARK_ECSPI_CTRL_BL_MASK (0xfff << 20)
- #define ARK_ECSPI_CONFIG 0x0c
- #define ARK_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
- #define ARK_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
- #define ARK_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
- #define ARK_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
- #define ARK_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
- #define ARK_ECSPI_INT 0x10
- #define ARK_ECSPI_INT_TEEN (1 << 0)
- #define ARK_ECSPI_INT_RREN (1 << 3)
- #define ARK_ECSPI_DMA 0x14
- #define ARK_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
- #define ARK_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
- #define ARK_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
- #define ARK_ECSPI_DMA_TEDEN (1 << 7)
- #define ARK_ECSPI_DMA_RXDEN (1 << 23)
- #define ARK_ECSPI_DMA_RXTDEN (1 << 31)
- #define ARK_ECSPI_STAT 0x18
- #define ARK_ECSPI_STAT_REN (1 << 8)
- #define ARK_ECSPI_STAT_RR (1 << 3)
- #define ARK_ECSPI_TESTREG 0x20
- #define ARK_ECSPI_TESTREG_LBC BIT(31)
- static void spi_ark_buf_rx_swap_u32(struct spi_ark_data *spi_ark)
- {
- unsigned int val = readl(spi_ark->base + ARK_ECSPI_RXDATA);
- if (spi_ark->rx_buf) {
- val &= spi_ark->word_mask;
- *(u32 *)spi_ark->rx_buf = val;
- spi_ark->rx_buf += sizeof(u32);
- }
- }
- static void spi_ark_buf_rx_swap(struct spi_ark_data *spi_ark)
- {
- unsigned int bytes_per_word;
- bytes_per_word = spi_ark_bytes_per_word(spi_ark->bits_per_word);
- if (spi_ark->read_u32) {
- spi_ark_buf_rx_swap_u32(spi_ark);
- return;
- }
- if (bytes_per_word == 1)
- spi_ark_buf_rx_u8(spi_ark);
- else if (bytes_per_word == 2)
- spi_ark_buf_rx_u16(spi_ark);
- }
- static void spi_ark_buf_tx_swap_u32(struct spi_ark_data *spi_ark)
- {
- u32 val = 0;
- if (spi_ark->tx_buf) {
- val = *(u32 *)spi_ark->tx_buf;
- val &= spi_ark->word_mask;
- spi_ark->tx_buf += sizeof(u32);
- }
- spi_ark->count -= sizeof(u32);
- writel(val, spi_ark->base + ARK_ECSPI_TXDATA);
- }
- static void spi_ark_buf_tx_swap(struct spi_ark_data *spi_ark)
- {
- u32 ctrl, val;
- unsigned int bytes_per_word;
- if (spi_ark->count == spi_ark->remainder) {
- ctrl = readl(spi_ark->base + ARK_ECSPI_CTRL);
- ctrl &= ~ARK_ECSPI_CTRL_BL_MASK;
- if (spi_ark->count > ARK_ECSPI_CTRL_MAX_BURST) {
- spi_ark->remainder = spi_ark->count %
- ARK_ECSPI_CTRL_MAX_BURST;
- val = ARK_ECSPI_CTRL_MAX_BURST * 8 - 1;
- } else if (spi_ark->count >= sizeof(u32)) {
- spi_ark->remainder = spi_ark->count % sizeof(u32);
- val = (spi_ark->count - spi_ark->remainder) * 8 - 1;
- } else {
- spi_ark->remainder = 0;
- val = spi_ark->bits_per_word - 1;
- spi_ark->read_u32 = 0;
- }
- ctrl |= (val << ARK_ECSPI_CTRL_BL_OFFSET);
- writel(ctrl, spi_ark->base + ARK_ECSPI_CTRL);
- }
- if (spi_ark->count >= sizeof(u32)) {
- spi_ark_buf_tx_swap_u32(spi_ark);
- return;
- }
- bytes_per_word = spi_ark_bytes_per_word(spi_ark->bits_per_word);
- if (bytes_per_word == 1)
- spi_ark_buf_tx_u8(spi_ark);
- else if (bytes_per_word == 2)
- spi_ark_buf_tx_u16(spi_ark);
- }
- /* ARK eCSPI */
- static unsigned int ark_ecspi_clkdiv(struct spi_ark_data *spi_ark,
- unsigned int fspi, unsigned int *fres)
- {
- /*
- * there are two 4-bit dividers, the pre-divider divides by
- * $pre, the post-divider by 2^$post
- */
- unsigned int pre, post;
- unsigned int fin = spi_ark->spi_clk;
- if (unlikely(fspi > fin))
- return 0;
- post = fls(fin) - fls(fspi);
- if (fin > fspi << post)
- post++;
- /* now we have: (fin <= fspi << post) with post being minimal */
- post = max(4U, post) - 4;
- if (unlikely(post > 0xf)) {
- dev_err(spi_ark->dev, "cannot set clock freq: %u (base freq: %u)\n",
- fspi, fin);
- return 0xff;
- }
- pre = DIV_ROUND_UP(fin, fspi << post) - 1;
- dev_dbg(spi_ark->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
- __func__, fin, fspi, post, pre);
- /* Resulting frequency for the SCLK line. */
- *fres = (fin / (pre + 1)) >> post;
- return (pre << ARK_ECSPI_CTRL_PREDIV_OFFSET) |
- (post << ARK_ECSPI_CTRL_POSTDIV_OFFSET);
- }
- static void ark_ecspi_intctrl(struct spi_ark_data *spi_ark, int enable)
- {
- unsigned val = 0;
- if (enable & ARK_INT_TE)
- val |= ARK_ECSPI_INT_TEEN;
- if (enable & ARK_INT_RR)
- val |= ARK_ECSPI_INT_RREN;
- writel(val, spi_ark->base + ARK_ECSPI_INT);
- }
- static void ark_ecspi_trigger(struct spi_ark_data *spi_ark)
- {
- u32 reg;
- reg = readl(spi_ark->base + ARK_ECSPI_CTRL);
- reg |= ARK_ECSPI_CTRL_XCH;
- writel(reg, spi_ark->base + ARK_ECSPI_CTRL);
- }
- static int ark_ecspi_config(struct spi_device *spi)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(spi->master);
- u32 ctrl = ARK_ECSPI_CTRL_ENABLE;
- u32 clk = spi_ark->speed_hz, delay, reg;
- u32 cfg = readl(spi_ark->base + ARK_ECSPI_CONFIG);
- /*
- * The hardware seems to have a race condition when changing modes. The
- * current assumption is that the selection of the channel arrives
- * earlier in the hardware than the mode bits when they are written at
- * the same time.
- * So set master mode for all channels as we do not support slave mode.
- */
- ctrl |= ARK_ECSPI_CTRL_MODE_MASK;
- /*
- * Enable SPI_RDY handling (falling edge/level triggered).
- */
- if (spi->mode & SPI_READY)
- ctrl |= ARK_ECSPI_CTRL_DRCTL(spi_ark->spi_drctl);
- /* set clock speed */
- ctrl |= ark_ecspi_clkdiv(spi_ark, spi_ark->speed_hz, &clk);
- spi_ark->spi_bus_clk = clk;
- /* set chip select to use */
- ctrl |= ARK_ECSPI_CTRL_CS(spi->chip_select);
- ctrl |= (spi_ark->bits_per_word - 1) << ARK_ECSPI_CTRL_BL_OFFSET;
- cfg |= ARK_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
- if (spi->mode & SPI_CPHA)
- cfg |= ARK_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
- else
- cfg &= ~ARK_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
- if (spi->mode & SPI_CPOL) {
- cfg |= ARK_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
- cfg |= ARK_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
- } else {
- cfg &= ~ARK_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
- cfg &= ~ARK_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
- }
- if (spi->mode & SPI_CS_HIGH)
- cfg |= ARK_ECSPI_CONFIG_SSBPOL(spi->chip_select);
- else
- cfg &= ~ARK_ECSPI_CONFIG_SSBPOL(spi->chip_select);
- if (spi_ark->usedma) {
- ctrl |= ARK_ECSPI_CTRL_SMC;
- }
- /* CTRL register always go first to bring out controller from reset */
- writel(ctrl, spi_ark->base + ARK_ECSPI_CTRL);
- reg = readl(spi_ark->base + ARK_ECSPI_TESTREG);
- if (spi->mode & SPI_LOOP)
- reg |= ARK_ECSPI_TESTREG_LBC;
- else
- reg &= ~ARK_ECSPI_TESTREG_LBC;
- writel(reg, spi_ark->base + ARK_ECSPI_TESTREG);
- writel(cfg, spi_ark->base + ARK_ECSPI_CONFIG);
- /*
- * Wait until the changes in the configuration register CONFIGREG
- * propagate into the hardware. It takes exactly one tick of the
- * SCLK clock, but we will wait two SCLK clock just to be sure. The
- * effect of the delay it takes for the hardware to apply changes
- * is noticable if the SCLK clock run very slow. In such a case, if
- * the polarity of SCLK should be inverted, the GPIO ChipSelect might
- * be asserted before the SCLK polarity changes, which would disrupt
- * the SPI communication as the device on the other end would consider
- * the change of SCLK polarity as a clock tick already.
- */
- delay = (2 * 1000000) / clk;
- if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
- udelay(delay);
- else /* SCLK is _very_ slow */
- usleep_range(delay, delay + 10);
- /* enable rx fifo */
- writel(ARK_ECSPI_STAT_REN, spi_ark->base + ARK_ECSPI_STAT);
- /*
- * Configure the DMA register: setup the watermark
- * and enable DMA request.
- */
- if (spi_ark->usedma)
- writel(ARK_ECSPI_DMA_RX_WML(spi_ark->wml) |
- ARK_ECSPI_DMA_TX_WML(spi_ark->wml) |
- ARK_ECSPI_DMA_RXT_WML(spi_ark->wml) |
- ARK_ECSPI_DMA_TEDEN | ARK_ECSPI_DMA_RXDEN |
- ARK_ECSPI_DMA_RXTDEN, spi_ark->base + ARK_ECSPI_DMA);
- else writel(0, spi_ark->base + ARK_ECSPI_DMA);
- return 0;
- }
- static int ark_ecspi_rx_available(struct spi_ark_data *spi_ark)
- {
- return readl(spi_ark->base + ARK_ECSPI_STAT) & ARK_ECSPI_STAT_RR;
- }
- static void ark_ecspi_reset(struct spi_ark_data *spi_ark)
- {
- /* drain receive buffer */
- while (ark_ecspi_rx_available(spi_ark))
- readl(spi_ark->base + ARK_ECSPI_RXDATA);
- }
- static struct spi_ark_devtype_data ark_ecspi_devtype_data = {
- .intctrl = ark_ecspi_intctrl,
- .config = ark_ecspi_config,
- .trigger = ark_ecspi_trigger,
- .rx_available = ark_ecspi_rx_available,
- .reset = ark_ecspi_reset,
- .fifo_size = 64,
- .has_dmamode = true,
- };
- static const struct of_device_id spi_ark_dt_ids[] = {
- { .compatible = "arkmicro,ark-ecspi", .data = &ark_ecspi_devtype_data, },
- { .compatible = "arkmicro,arke-ecspi", .data = &ark_ecspi_devtype_data, },
- { /* sentinel */ }
- };
- MODULE_DEVICE_TABLE(of, spi_ark_dt_ids);
- static void spi_ark_chipselect(struct spi_device *spi, int is_active)
- {
- int active = is_active != BITBANG_CS_INACTIVE;
- int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
- if (spi->mode & SPI_NO_CS)
- return;
- if (!gpio_is_valid(spi->cs_gpio))
- return;
- gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
- }
- static void spi_ark_push(struct spi_ark_data *spi_ark)
- {
- while (spi_ark->txfifo < spi_ark->devtype_data->fifo_size) {
- if (!spi_ark->count)
- break;
- if (spi_ark->txfifo && (spi_ark->count == spi_ark->remainder))
- break;
- spi_ark->tx(spi_ark);
- spi_ark->txfifo++;
- }
- spi_ark->devtype_data->trigger(spi_ark);
- }
- static irqreturn_t spi_ark_isr(int irq, void *dev_id)
- {
- struct spi_ark_data *spi_ark = dev_id;
- while (spi_ark->devtype_data->rx_available(spi_ark)) {
- spi_ark->rx(spi_ark);
- spi_ark->txfifo--;
- }
- if (spi_ark->count) {
- spi_ark_push(spi_ark);
- return IRQ_HANDLED;
- }
- if (spi_ark->txfifo) {
- /* No data left to push, but still waiting for rx data,
- * enable receive data available interrupt.
- */
- spi_ark->devtype_data->intctrl(
- spi_ark, ARK_INT_RR);
- return IRQ_HANDLED;
- }
- spi_ark->devtype_data->intctrl(spi_ark, 0);
- complete(&spi_ark->xfer_done);
- return IRQ_HANDLED;
- }
- static int spi_ark_dma_configure(struct spi_master *master)
- {
- int ret;
- struct dma_slave_config rx = {}, tx = {};
- struct spi_ark_data *spi_ark = spi_master_get_devdata(master);
- spi_ark->bits_per_word = 32;
- tx.direction = DMA_MEM_TO_DEV;
- tx.dst_addr = spi_ark->base_phys + ARK_ECSPI_TXDATA;
- tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- tx.dst_maxburst = spi_ark->wml;
- tx.device_fc = false;
- ret = dmaengine_slave_config(master->dma_tx, &tx);
- if (ret) {
- dev_err(spi_ark->dev, "TX dma configuration failed with %d\n", ret);
- return ret;
- }
- rx.direction = DMA_DEV_TO_MEM;
- rx.src_addr = spi_ark->base_phys + ARK_ECSPI_RXDATA;
- rx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- rx.src_maxburst = spi_ark->wml;
- rx.device_fc = false;
- ret = dmaengine_slave_config(master->dma_rx, &rx);
- if (ret) {
- dev_err(spi_ark->dev, "RX dma configuration failed with %d\n", ret);
- return ret;
- }
- return 0;
- }
- static int spi_ark_setupxfer(struct spi_device *spi,
- struct spi_transfer *t)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(spi->master);
- u32 mask;
- int ret;
- if (!t)
- return 0;
- spi_ark->bits_per_word = t->bits_per_word;
- spi_ark->speed_hz = t->speed_hz;
- /* Initialize the functions for transfer */
- spi_ark->remainder = 0;
- spi_ark->read_u32 = 1;
- mask = (1 << spi_ark->bits_per_word) - 1;
- spi_ark->rx = spi_ark_buf_rx_swap;
- spi_ark->tx = spi_ark_buf_tx_swap;
- spi_ark->remainder = t->len;
- if (spi_ark->bits_per_word <= 8)
- spi_ark->word_mask = mask << 24 | mask << 16
- | mask << 8 | mask;
- else if (spi_ark->bits_per_word <= 16)
- spi_ark->word_mask = mask << 16 | mask;
- else
- spi_ark->word_mask = mask;
- if (spi_ark_can_dma(spi_ark->bitbang.master, spi, t))
- spi_ark->usedma = 1;
- else
- spi_ark->usedma = 0;
- if (spi_ark->usedma) {
- ret = spi_ark_dma_configure(spi->master);
- if (ret)
- return ret;
- }
- spi_ark->devtype_data->config(spi);
- return 0;
- }
- static void spi_ark_sdma_exit(struct spi_ark_data *spi_ark)
- {
- struct spi_master *master = spi_ark->bitbang.master;
- if (master->dma_rx) {
- dma_release_channel(master->dma_rx);
- master->dma_rx = NULL;
- }
- if (master->dma_tx) {
- dma_release_channel(master->dma_tx);
- master->dma_tx = NULL;
- }
- }
- static int spi_ark_sdma_init(struct device *dev, struct spi_ark_data *spi_ark,
- struct spi_master *master)
- {
- int ret;
- spi_ark->wml = spi_ark->devtype_data->fifo_size / 2;
- /* Prepare for TX DMA: */
- master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
- if (IS_ERR(master->dma_tx)) {
- ret = PTR_ERR(master->dma_tx);
- dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
- master->dma_tx = NULL;
- goto err;
- }
- /* Prepare for RX : */
- master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
- if (IS_ERR(master->dma_rx)) {
- ret = PTR_ERR(master->dma_rx);
- dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
- master->dma_rx = NULL;
- goto err;
- }
- init_completion(&spi_ark->dma_rx_completion);
- init_completion(&spi_ark->dma_tx_completion);
- master->can_dma = spi_ark_can_dma;
- master->max_dma_len = MAX_SDMA_BD_BYTES;
- spi_ark->bitbang.master->flags = SPI_MASTER_MUST_RX |
- SPI_MASTER_MUST_TX;
- return 0;
- err:
- spi_ark_sdma_exit(spi_ark);
- return ret;
- }
- static void spi_ark_dma_rx_callback(void *cookie)
- {
- struct spi_ark_data *spi_ark = (struct spi_ark_data *)cookie;
- /* Invalidate cache after read */
- dma_sync_sg_for_cpu(spi_ark->dev,
- spi_ark->dma_rx_sg->sgl,
- spi_ark->dma_rx_sg->nents,
- DMA_FROM_DEVICE);
- complete(&spi_ark->dma_rx_completion);
- }
- static void spi_ark_dma_tx_callback(void *cookie)
- {
- struct spi_ark_data *spi_ark = (struct spi_ark_data *)cookie;
- complete(&spi_ark->dma_tx_completion);
- }
- static int spi_ark_calculate_timeout(struct spi_ark_data *spi_ark, int size)
- {
- unsigned long timeout = 0;
- /* Time with actual data transfer and CS change delay related to HW */
- timeout = (8 + 4) * size / spi_ark->spi_bus_clk;
- /* Add extra second for scheduler related activities */
- timeout += 1;
- /* Double calculated timeout */
- return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
- }
- static int spi_ark_dma_transfer(struct spi_ark_data *spi_ark,
- struct spi_transfer *transfer)
- {
- struct dma_async_tx_descriptor *desc_tx, *desc_rx;
- unsigned long transfer_timeout;
- unsigned long timeout;
- struct spi_master *master = spi_ark->bitbang.master;
- struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
- /*
- * The TX DMA setup starts the transfer, so make sure RX is configured
- * before TX.
- */
- spi_ark->dma_rx_sg = rx;
- desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
- rx->sgl, rx->nents, DMA_DEV_TO_MEM,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!desc_rx)
- return -EINVAL;
- desc_rx->callback = spi_ark_dma_rx_callback;
- desc_rx->callback_param = (void *)spi_ark;
- dmaengine_submit(desc_rx);
- reinit_completion(&spi_ark->dma_rx_completion);
- dma_async_issue_pending(master->dma_rx);
- desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
- tx->sgl, tx->nents, DMA_MEM_TO_DEV,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!desc_tx) {
- dmaengine_terminate_all(master->dma_tx);
- return -EINVAL;
- }
- desc_tx->callback = spi_ark_dma_tx_callback;
- desc_tx->callback_param = (void *)spi_ark;
- dmaengine_submit(desc_tx);
- reinit_completion(&spi_ark->dma_tx_completion);
- /* Flush cache before write */
- dma_sync_sg_for_device(spi_ark->dev, tx->sgl,
- tx->nents, DMA_TO_DEVICE);
- dma_async_issue_pending(master->dma_tx);
- transfer_timeout = spi_ark_calculate_timeout(spi_ark, transfer->len);
- /* Wait SDMA to finish the data transfer.*/
- timeout = wait_for_completion_timeout(&spi_ark->dma_tx_completion,
- transfer_timeout);
- if (!timeout) {
- dev_err(spi_ark->dev, "I/O Error in DMA TX\n");
- dmaengine_terminate_all(master->dma_tx);
- dmaengine_terminate_all(master->dma_rx);
- return -ETIMEDOUT;
- }
- timeout = wait_for_completion_timeout(&spi_ark->dma_rx_completion,
- transfer_timeout);
- if (!timeout) {
- dev_err(&master->dev, "I/O Error in DMA RX\n");
- spi_ark->devtype_data->reset(spi_ark);
- dmaengine_terminate_all(master->dma_rx);
- return -ETIMEDOUT;
- }
- return transfer->len;
- }
- static int spi_ark_pio_xfer(struct spi_device *spi,
- struct spi_transfer *transfer)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(spi->master);
- unsigned long transfer_timeout;
- unsigned long timeout;
- spi_ark->tx_buf = transfer->tx_buf;
- spi_ark->rx_buf = transfer->rx_buf;
- spi_ark->count = transfer->len;
- spi_ark->txfifo = 0;
- reinit_completion(&spi_ark->xfer_done);
- spi_ark_push(spi_ark);
- spi_ark->devtype_data->intctrl(spi_ark, ARK_INT_TE);
- transfer_timeout = spi_ark_calculate_timeout(spi_ark, transfer->len);
- timeout = wait_for_completion_timeout(&spi_ark->xfer_done,
- transfer_timeout);
- if (!timeout) {
- dev_err(&spi->dev, "I/O Error in PIO\n");
- spi_ark->devtype_data->reset(spi_ark);
- return -ETIMEDOUT;
- }
- return transfer->len;
- }
- static int spi_ark_transfer(struct spi_device *spi,
- struct spi_transfer *transfer)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(spi->master);
- int ret;
- if (spi_ark->usedma) {
- if ((ret = spi_ark_dma_transfer(spi_ark, &spi_ark->dma_transfer)) < 0)
- return ret;
- if (spi_ark->pio_transfer.len > 0 &&
- (ret = spi_ark_pio_xfer(spi, &spi_ark->pio_transfer)) < 0)
- return ret;
- return transfer->len;
- }
- else {
- return spi_ark_pio_xfer(spi, transfer);
- }
- }
- static int spi_ark_setup(struct spi_device *spi)
- {
- dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
- spi->mode, spi->bits_per_word, spi->max_speed_hz);
- if (spi->mode & SPI_NO_CS)
- return 0;
- if (gpio_is_valid(spi->cs_gpio))
- gpio_direction_output(spi->cs_gpio,
- spi->mode & SPI_CS_HIGH ? 0 : 1);
- spi_ark_chipselect(spi, BITBANG_CS_INACTIVE);
- return 0;
- }
- static void spi_ark_cleanup(struct spi_device *spi)
- {
- }
- static int
- spi_ark_prepare_message(struct spi_master *master, struct spi_message *msg)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(master);
- int ret;
- ret = clk_enable(spi_ark->clk_per);
- if (ret)
- return ret;
- ret = clk_enable(spi_ark->clk_ipg);
- if (ret) {
- clk_disable(spi_ark->clk_per);
- return ret;
- }
- return 0;
- }
- static int
- spi_ark_unprepare_message(struct spi_master *master, struct spi_message *msg)
- {
- struct spi_ark_data *spi_ark = spi_master_get_devdata(master);
- clk_disable(spi_ark->clk_ipg);
- clk_disable(spi_ark->clk_per);
- return 0;
- }
- static int spi_ark_probe(struct platform_device *pdev)
- {
- struct device_node *np = pdev->dev.of_node;
- const struct of_device_id *of_id =
- of_match_device(spi_ark_dt_ids, &pdev->dev);
- struct spi_master *master;
- struct spi_ark_data *spi_ark;
- struct resource *res;
- int i, ret, irq, spi_drctl;
- u32 num_chipselect, cs_gpio;
- if (!np) {
- dev_err(&pdev->dev, "can't get the platform data\n");
- return -EINVAL;
- }
- master = spi_alloc_master(&pdev->dev, sizeof(struct spi_ark_data));
- if (!master)
- return -ENOMEM;
- ret = of_property_read_u32(np, "ark,spi-rdy-drctl", &spi_drctl);
- if ((ret < 0) || (spi_drctl >= 0x3)) {
- /* '11' is reserved */
- spi_drctl = 0;
- }
- platform_set_drvdata(pdev, master);
- master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
- master->bus_num = np ? -1 : pdev->id;
- spi_ark = spi_master_get_devdata(master);
- spi_ark->bitbang.master = master;
- spi_ark->dev = &pdev->dev;
- spi_ark->devtype_data = of_id ? of_id->data :
- (struct spi_ark_devtype_data *)pdev->id_entry->driver_data;
- ret = of_property_read_u32(np, "num-chipselect", &num_chipselect);
- if (ret < 0) {
- dev_err(&pdev->dev, "can't get num-chipselect: %d\n", ret);
- goto out_master_put;
- }
- master->num_chipselect = num_chipselect;
- master->cs_gpios = devm_kzalloc(&master->dev,
- sizeof(int) * master->num_chipselect, GFP_KERNEL);
- if (!master->cs_gpios)
- return -ENOMEM;
- if (of_device_is_compatible(pdev->dev.of_node, "arkmicro,arke-ecspi"))
- spi_ark->is_arke = true;
- else
- spi_ark->is_arke = false;
- for (i = 0; i < master->num_chipselect; i++) {
- of_property_read_u32_index(np, "chipselects", i, &cs_gpio);
- master->cs_gpios[i] = cs_gpio;
- }
- spi_ark->bitbang.chipselect = spi_ark_chipselect;
- spi_ark->bitbang.setup_transfer = spi_ark_setupxfer;
- spi_ark->bitbang.txrx_bufs = spi_ark_transfer;
- spi_ark->bitbang.master->setup = spi_ark_setup;
- spi_ark->bitbang.master->cleanup = spi_ark_cleanup;
- spi_ark->bitbang.master->prepare_message = spi_ark_prepare_message;
- spi_ark->bitbang.master->unprepare_message = spi_ark_unprepare_message;
- spi_ark->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
- | SPI_NO_CS;
- spi_ark->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY;
- spi_ark->spi_drctl = spi_drctl;
- init_completion(&spi_ark->xfer_done);
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- spi_ark->base = devm_ioremap_resource(&pdev->dev, res);
- if (IS_ERR(spi_ark->base)) {
- ret = PTR_ERR(spi_ark->base);
- goto out_master_put;
- }
- spi_ark->base_phys = res->start;
- irq = platform_get_irq(pdev, 0);
- if (irq < 0) {
- ret = irq;
- goto out_master_put;
- }
- ret = devm_request_irq(&pdev->dev, irq, spi_ark_isr, 0,
- dev_name(&pdev->dev), spi_ark);
- if (ret) {
- dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
- goto out_master_put;
- }
- spi_ark->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
- if (IS_ERR(spi_ark->clk_ipg)) {
- ret = PTR_ERR(spi_ark->clk_ipg);
- goto out_master_put;
- }
- spi_ark->clk_per = devm_clk_get(&pdev->dev, "per");
- if (IS_ERR(spi_ark->clk_per)) {
- ret = PTR_ERR(spi_ark->clk_per);
- goto out_master_put;
- }
- ret = clk_prepare_enable(spi_ark->clk_per);
- if (ret)
- goto out_master_put;
- ret = clk_prepare_enable(spi_ark->clk_ipg);
- if (ret)
- goto out_put_per;
- spi_ark->spi_clk = clk_get_rate(spi_ark->clk_per);
- if (spi_ark->devtype_data->has_dmamode) {
- ret = spi_ark_sdma_init(&pdev->dev, spi_ark, master);
- if (ret == -EPROBE_DEFER)
- goto out_clk_put;
- if (ret < 0)
- dev_err(&pdev->dev, "dma setup error %d, use pio\n",
- ret);
- }
- spi_ark->devtype_data->reset(spi_ark);
- spi_ark->devtype_data->intctrl(spi_ark, 0);
- master->dev.of_node = pdev->dev.of_node;
- ret = spi_bitbang_start(&spi_ark->bitbang);
- if (ret) {
- dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
- goto out_clk_put;
- }
- if (!master->cs_gpios) {
- dev_err(&pdev->dev, "No CS GPIOs available\n");
- ret = -EINVAL;
- goto out_clk_put;
- }
- for (i = 0; i < master->num_chipselect; i++) {
- if (!gpio_is_valid(master->cs_gpios[i]))
- continue;
- ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
- DRIVER_NAME);
- if (ret) {
- dev_err(&pdev->dev, "Can't get CS GPIO %i err = %d.\n",
- master->cs_gpios[i], ret);
- goto out_clk_put;
- }
- }
- dev_info(&pdev->dev, "probed\n");
- clk_disable(spi_ark->clk_ipg);
- clk_disable(spi_ark->clk_per);
- return ret;
- out_clk_put:
- clk_disable_unprepare(spi_ark->clk_ipg);
- out_put_per:
- clk_disable_unprepare(spi_ark->clk_per);
- out_master_put:
- spi_master_put(master);
- return ret;
- }
- static int spi_ark_remove(struct platform_device *pdev)
- {
- struct spi_master *master = platform_get_drvdata(pdev);
- struct spi_ark_data *spi_ark = spi_master_get_devdata(master);
- int ret;
- spi_bitbang_stop(&spi_ark->bitbang);
- ret = clk_enable(spi_ark->clk_per);
- if (ret)
- return ret;
- ret = clk_enable(spi_ark->clk_ipg);
- if (ret) {
- clk_disable(spi_ark->clk_per);
- return ret;
- }
- writel(0, spi_ark->base + ARK_ECSPI_CTRL);
- clk_disable_unprepare(spi_ark->clk_ipg);
- clk_disable_unprepare(spi_ark->clk_per);
- spi_ark_sdma_exit(spi_ark);
- spi_master_put(master);
- return 0;
- }
- static struct platform_driver spi_ark_driver = {
- .driver = {
- .name = DRIVER_NAME,
- .of_match_table = spi_ark_dt_ids,
- },
- .probe = spi_ark_probe,
- .remove = spi_ark_remove,
- };
- module_platform_driver(spi_ark_driver);
- MODULE_DESCRIPTION("SPI Master Controller driver");
- MODULE_AUTHOR("Sim");
- MODULE_LICENSE("GPL v2");
- MODULE_ALIAS("platform:" DRIVER_NAME);
|