11b2fd5bfSSimon Glass /*
21b2fd5bfSSimon Glass * spi driver for rockchip
31b2fd5bfSSimon Glass *
41b2fd5bfSSimon Glass * (C) Copyright 2015 Google, Inc
51b2fd5bfSSimon Glass *
61b2fd5bfSSimon Glass * (C) Copyright 2008-2013 Rockchip Electronics
71b2fd5bfSSimon Glass * Peter, Software Engineering, <superpeter.cai@gmail.com>.
81b2fd5bfSSimon Glass *
91b2fd5bfSSimon Glass * SPDX-License-Identifier: GPL-2.0+
101b2fd5bfSSimon Glass */
111b2fd5bfSSimon Glass
121b2fd5bfSSimon Glass #include <common.h>
131b2fd5bfSSimon Glass #include <clk.h>
141b2fd5bfSSimon Glass #include <dm.h>
156e019c4fSSimon Glass #include <dt-structs.h>
161b2fd5bfSSimon Glass #include <errno.h>
171b2fd5bfSSimon Glass #include <spi.h>
181221ce45SMasahiro Yamada #include <linux/errno.h>
191b2fd5bfSSimon Glass #include <asm/io.h>
201b2fd5bfSSimon Glass #include <asm/arch/clock.h>
211b2fd5bfSSimon Glass #include <asm/arch/periph.h>
221b2fd5bfSSimon Glass #include <dm/pinctrl.h>
231b2fd5bfSSimon Glass #include "rk_spi.h"
241b2fd5bfSSimon Glass
251b2fd5bfSSimon Glass DECLARE_GLOBAL_DATA_PTR;
261b2fd5bfSSimon Glass
271b2fd5bfSSimon Glass /* Change to 1 to output registers at the start of each transaction */
281b2fd5bfSSimon Glass #define DEBUG_RK_SPI 0
291b2fd5bfSSimon Glass
307d984abeSJon Lin struct rockchip_spi_quirks {
317d984abeSJon Lin u32 max_baud_div_in_cpha;
327d984abeSJon Lin };
337d984abeSJon Lin
341b2fd5bfSSimon Glass struct rockchip_spi_platdata {
356e019c4fSSimon Glass #if CONFIG_IS_ENABLED(OF_PLATDATA)
366e019c4fSSimon Glass struct dtd_rockchip_rk3288_spi of_plat;
376e019c4fSSimon Glass #endif
381b2fd5bfSSimon Glass s32 frequency; /* Default clock frequency, -1 for none */
391b2fd5bfSSimon Glass fdt_addr_t base;
401b2fd5bfSSimon Glass uint deactivate_delay_us; /* Delay to wait after deactivate */
41183a3a0fSSimon Glass uint activate_delay_us; /* Delay to wait after activate */
421b2fd5bfSSimon Glass };
431b2fd5bfSSimon Glass
441b2fd5bfSSimon Glass struct rockchip_spi_priv {
451b2fd5bfSSimon Glass struct rockchip_spi *regs;
46135aa950SStephen Warren struct clk clk;
471b2fd5bfSSimon Glass unsigned int max_freq;
481b2fd5bfSSimon Glass unsigned int mode;
491b2fd5bfSSimon Glass ulong last_transaction_us; /* Time of last transaction end */
501b2fd5bfSSimon Glass u8 bits_per_word; /* max 16 bits per word */
511b2fd5bfSSimon Glass u8 n_bytes;
521b2fd5bfSSimon Glass unsigned int speed_hz;
5328a943c1SSimon Glass unsigned int last_speed_hz;
541b2fd5bfSSimon Glass uint input_rate;
5581a1596cSJon Lin uint cr0;
5664993c87SJon Lin u32 rsd; /* Rx sample delay cycles */
577d984abeSJon Lin
587d984abeSJon Lin /* quirks */
597d984abeSJon Lin u32 max_baud_div_in_cpha;
601b2fd5bfSSimon Glass };
611b2fd5bfSSimon Glass
621b2fd5bfSSimon Glass #define SPI_FIFO_DEPTH 32
6364993c87SJon Lin #define SPI_CR0_RSD_MAX 0x3
641b2fd5bfSSimon Glass
rkspi_dump_regs(struct rockchip_spi * regs)651b2fd5bfSSimon Glass static void rkspi_dump_regs(struct rockchip_spi *regs)
661b2fd5bfSSimon Glass {
671b2fd5bfSSimon Glass debug("ctrl0: \t\t0x%08x\n", readl(®s->ctrlr0));
681b2fd5bfSSimon Glass debug("ctrl1: \t\t0x%08x\n", readl(®s->ctrlr1));
691b2fd5bfSSimon Glass debug("ssienr: \t\t0x%08x\n", readl(®s->enr));
701b2fd5bfSSimon Glass debug("ser: \t\t0x%08x\n", readl(®s->ser));
711b2fd5bfSSimon Glass debug("baudr: \t\t0x%08x\n", readl(®s->baudr));
721b2fd5bfSSimon Glass debug("txftlr: \t\t0x%08x\n", readl(®s->txftlr));
731b2fd5bfSSimon Glass debug("rxftlr: \t\t0x%08x\n", readl(®s->rxftlr));
741b2fd5bfSSimon Glass debug("txflr: \t\t0x%08x\n", readl(®s->txflr));
751b2fd5bfSSimon Glass debug("rxflr: \t\t0x%08x\n", readl(®s->rxflr));
761b2fd5bfSSimon Glass debug("sr: \t\t0x%08x\n", readl(®s->sr));
771b2fd5bfSSimon Glass debug("imr: \t\t0x%08x\n", readl(®s->imr));
781b2fd5bfSSimon Glass debug("isr: \t\t0x%08x\n", readl(®s->isr));
791b2fd5bfSSimon Glass debug("dmacr: \t\t0x%08x\n", readl(®s->dmacr));
801b2fd5bfSSimon Glass debug("dmatdlr: \t0x%08x\n", readl(®s->dmatdlr));
811b2fd5bfSSimon Glass debug("dmardlr: \t0x%08x\n", readl(®s->dmardlr));
821b2fd5bfSSimon Glass }
831b2fd5bfSSimon Glass
rkspi_enable_chip(struct rockchip_spi * regs,bool enable)841b2fd5bfSSimon Glass static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
851b2fd5bfSSimon Glass {
861b2fd5bfSSimon Glass writel(enable ? 1 : 0, ®s->enr);
871b2fd5bfSSimon Glass }
881b2fd5bfSSimon Glass
rkspi_set_clk(struct rockchip_spi_priv * priv,uint speed)891b2fd5bfSSimon Glass static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
901b2fd5bfSSimon Glass {
919fc354e2SPhilipp Tomsich /*
929fc354e2SPhilipp Tomsich * We should try not to exceed the speed requested by the caller:
939fc354e2SPhilipp Tomsich * when selecting a divider, we need to make sure we round up.
949fc354e2SPhilipp Tomsich */
959fc354e2SPhilipp Tomsich uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
961b2fd5bfSSimon Glass
979fc354e2SPhilipp Tomsich /* The baudrate register (BAUDR) is defined as a 32bit register where
989fc354e2SPhilipp Tomsich * the upper 16bit are reserved and having 'Fsclk_out' in the lower
999fc354e2SPhilipp Tomsich * 16bits with 'Fsclk_out' defined as follows:
1009fc354e2SPhilipp Tomsich *
1019fc354e2SPhilipp Tomsich * Fsclk_out = Fspi_clk/ SCKDV
1029fc354e2SPhilipp Tomsich * Where SCKDV is any even value between 2 and 65534.
1039fc354e2SPhilipp Tomsich */
1049fc354e2SPhilipp Tomsich if (clk_div > 0xfffe) {
1059fc354e2SPhilipp Tomsich clk_div = 0xfffe;
106204b4e68SHeinrich Schuchardt debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
1079fc354e2SPhilipp Tomsich __func__, speed, priv->input_rate / clk_div);
1089fc354e2SPhilipp Tomsich }
1099fc354e2SPhilipp Tomsich
1109fc354e2SPhilipp Tomsich /* Round up to the next even 16bit number */
1119fc354e2SPhilipp Tomsich clk_div = (clk_div + 1) & 0xfffe;
1129fc354e2SPhilipp Tomsich
1131b2fd5bfSSimon Glass debug("spi speed %u, div %u\n", speed, clk_div);
1141b2fd5bfSSimon Glass
1157d984abeSJon Lin /* the maxmum divisor is 4 for mode1/3 spi master case for quirks */
1167d984abeSJon Lin if (priv->max_baud_div_in_cpha && clk_div > priv->max_baud_div_in_cpha && priv->mode & SPI_CPHA) {
1177d984abeSJon Lin clk_div = priv->max_baud_div_in_cpha;
1187d984abeSJon Lin clk_set_rate(&priv->clk, 4 * speed);
1197d984abeSJon Lin speed = clk_get_rate(&priv->clk);
1207d984abeSJon Lin }
1219fc354e2SPhilipp Tomsich clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
12228a943c1SSimon Glass priv->last_speed_hz = speed;
1231b2fd5bfSSimon Glass }
1241b2fd5bfSSimon Glass
rkspi_wait_till_not_busy(struct rockchip_spi * regs)1251b2fd5bfSSimon Glass static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
1261b2fd5bfSSimon Glass {
1271b2fd5bfSSimon Glass unsigned long start;
1281b2fd5bfSSimon Glass
1291b2fd5bfSSimon Glass start = get_timer(0);
1301b2fd5bfSSimon Glass while (readl(®s->sr) & SR_BUSY) {
1311b2fd5bfSSimon Glass if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
1321b2fd5bfSSimon Glass debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
1331b2fd5bfSSimon Glass return -ETIMEDOUT;
1341b2fd5bfSSimon Glass }
1351b2fd5bfSSimon Glass }
1361b2fd5bfSSimon Glass
1371b2fd5bfSSimon Glass return 0;
1381b2fd5bfSSimon Glass }
1391b2fd5bfSSimon Glass
spi_cs_activate(struct udevice * dev,uint cs)140183a3a0fSSimon Glass static void spi_cs_activate(struct udevice *dev, uint cs)
1411b2fd5bfSSimon Glass {
142183a3a0fSSimon Glass struct udevice *bus = dev->parent;
143183a3a0fSSimon Glass struct rockchip_spi_platdata *plat = bus->platdata;
144183a3a0fSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
145183a3a0fSSimon Glass struct rockchip_spi *regs = priv->regs;
146183a3a0fSSimon Glass
147b4252474SSimon Glass /* If it's too soon to do another transaction, wait */
148b4252474SSimon Glass if (plat->deactivate_delay_us && priv->last_transaction_us) {
149b4252474SSimon Glass ulong delay_us; /* The delay completed so far */
150b4252474SSimon Glass delay_us = timer_get_us() - priv->last_transaction_us;
151b4252474SSimon Glass if (delay_us < plat->deactivate_delay_us)
152b4252474SSimon Glass udelay(plat->deactivate_delay_us - delay_us);
153b4252474SSimon Glass }
154b4252474SSimon Glass
1551b2fd5bfSSimon Glass debug("activate cs%u\n", cs);
1561b2fd5bfSSimon Glass writel(1 << cs, ®s->ser);
157183a3a0fSSimon Glass if (plat->activate_delay_us)
158183a3a0fSSimon Glass udelay(plat->activate_delay_us);
1591b2fd5bfSSimon Glass }
1601b2fd5bfSSimon Glass
spi_cs_deactivate(struct udevice * dev,uint cs)161183a3a0fSSimon Glass static void spi_cs_deactivate(struct udevice *dev, uint cs)
1621b2fd5bfSSimon Glass {
163183a3a0fSSimon Glass struct udevice *bus = dev->parent;
164183a3a0fSSimon Glass struct rockchip_spi_platdata *plat = bus->platdata;
165183a3a0fSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
166183a3a0fSSimon Glass struct rockchip_spi *regs = priv->regs;
167183a3a0fSSimon Glass
1681b2fd5bfSSimon Glass debug("deactivate cs%u\n", cs);
1691b2fd5bfSSimon Glass writel(0, ®s->ser);
170183a3a0fSSimon Glass
171183a3a0fSSimon Glass /* Remember time of this transaction so we can honour the bus delay */
172183a3a0fSSimon Glass if (plat->deactivate_delay_us)
173183a3a0fSSimon Glass priv->last_transaction_us = timer_get_us();
1741b2fd5bfSSimon Glass }
1751b2fd5bfSSimon Glass
1766e019c4fSSimon Glass #if CONFIG_IS_ENABLED(OF_PLATDATA)
conv_of_platdata(struct udevice * dev)1776e019c4fSSimon Glass static int conv_of_platdata(struct udevice *dev)
1786e019c4fSSimon Glass {
1796e019c4fSSimon Glass struct rockchip_spi_platdata *plat = dev->platdata;
1806e019c4fSSimon Glass struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
1816e019c4fSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(dev);
1826e019c4fSSimon Glass int ret;
1836e019c4fSSimon Glass
1846e019c4fSSimon Glass plat->base = dtplat->reg[0];
1856e019c4fSSimon Glass plat->frequency = 20000000;
1866e019c4fSSimon Glass ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
1876e019c4fSSimon Glass if (ret < 0)
1886e019c4fSSimon Glass return ret;
1896e019c4fSSimon Glass dev->req_seq = 0;
1906e019c4fSSimon Glass
1916e019c4fSSimon Glass return 0;
1926e019c4fSSimon Glass }
1936e019c4fSSimon Glass #endif
1946e019c4fSSimon Glass
rockchip_spi_ofdata_to_platdata(struct udevice * bus)1951b2fd5bfSSimon Glass static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
1961b2fd5bfSSimon Glass {
1976e019c4fSSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1986e019c4fSSimon Glass struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
19971037d1cSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
20064993c87SJon Lin u32 rsd_nsecs;
2011b2fd5bfSSimon Glass int ret;
2021b2fd5bfSSimon Glass
203a9db6e69SPhilipp Tomsich plat->base = dev_read_addr(bus);
2041b2fd5bfSSimon Glass
20571037d1cSSimon Glass ret = clk_get_by_index(bus, 0, &priv->clk);
20671037d1cSSimon Glass if (ret < 0) {
20771037d1cSSimon Glass debug("%s: Could not get clock for %s: %d\n", __func__,
20871037d1cSSimon Glass bus->name, ret);
20971037d1cSSimon Glass return ret;
21071037d1cSSimon Glass }
2111b2fd5bfSSimon Glass
2126c65577cSPhilipp Tomsich plat->frequency =
2136c65577cSPhilipp Tomsich dev_read_u32_default(bus, "spi-max-frequency", 50000000);
2146c65577cSPhilipp Tomsich plat->deactivate_delay_us =
2156c65577cSPhilipp Tomsich dev_read_u32_default(bus, "spi-deactivate-delay", 0);
2166c65577cSPhilipp Tomsich plat->activate_delay_us =
2176c65577cSPhilipp Tomsich dev_read_u32_default(bus, "spi-activate-delay", 0);
2186c65577cSPhilipp Tomsich
21964993c87SJon Lin rsd_nsecs = dev_read_u32_default(bus, "rx-sample-delay-ns", 0);
22064993c87SJon Lin if (rsd_nsecs > 0) {
22164993c87SJon Lin u32 spi_clk, rsd;
22264993c87SJon Lin
22364993c87SJon Lin spi_clk = clk_get_rate(&priv->clk);
22464993c87SJon Lin /* rx sample delay is expressed in parent clock cycles (max 3) */
22564993c87SJon Lin rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (spi_clk >> 8), 1000000000 >> 8);
22664993c87SJon Lin if (!rsd) {
22764993c87SJon Lin pr_err("SPI spi_clk %dHz are too slow to express %u ns delay\n", spi_clk, rsd_nsecs);
22864993c87SJon Lin } else if (rsd > SPI_CR0_RSD_MAX) {
22964993c87SJon Lin rsd = SPI_CR0_RSD_MAX;
23064993c87SJon Lin pr_err("SPI spi_clk %dHz are too fast to express %u ns delay, clamping at %u ns\n",
23164993c87SJon Lin spi_clk, rsd_nsecs, SPI_CR0_RSD_MAX * 1000000000U / spi_clk);
23264993c87SJon Lin }
23364993c87SJon Lin priv->rsd = rsd;
23464993c87SJon Lin }
23564993c87SJon Lin
23664993c87SJon Lin debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d rsd=%d\n",
23790a28470SSimon Glass __func__, (uint)plat->base, plat->frequency,
23864993c87SJon Lin plat->deactivate_delay_us, priv->rsd);
2396e019c4fSSimon Glass #endif
2401b2fd5bfSSimon Glass
2411b2fd5bfSSimon Glass return 0;
2421b2fd5bfSSimon Glass }
2431b2fd5bfSSimon Glass
rockchip_spi_calc_modclk(ulong max_freq)244bd376714SPhilipp Tomsich static int rockchip_spi_calc_modclk(ulong max_freq)
245bd376714SPhilipp Tomsich {
246d16120a6SPhilipp Tomsich /*
247d16120a6SPhilipp Tomsich * While this is not strictly correct for the RK3368, as the
248d16120a6SPhilipp Tomsich * GPLL will be 576MHz, things will still work, as the
249d16120a6SPhilipp Tomsich * clk_set_rate(...) implementation in our clock-driver will
250d16120a6SPhilipp Tomsich * chose the next closest rate not exceeding what we request
251d16120a6SPhilipp Tomsich * based on the output of this function.
252d16120a6SPhilipp Tomsich */
253d16120a6SPhilipp Tomsich
254bd376714SPhilipp Tomsich unsigned div;
255bd376714SPhilipp Tomsich const unsigned long gpll_hz = 594000000UL;
256bd376714SPhilipp Tomsich
257bd376714SPhilipp Tomsich /*
258bd376714SPhilipp Tomsich * We need to find an input clock that provides at least twice
259bd376714SPhilipp Tomsich * the maximum frequency and can be generated from the assumed
260bd376714SPhilipp Tomsich * speed of GPLL (594MHz) using an integer divider.
261bd376714SPhilipp Tomsich *
262bd376714SPhilipp Tomsich * To give us more achievable bitrates at higher speeds (these
263bd376714SPhilipp Tomsich * are generated by dividing by an even 16-bit integer from
264bd376714SPhilipp Tomsich * this frequency), we try to have an input frequency of at
265bd376714SPhilipp Tomsich * least 4x our max_freq.
266bd376714SPhilipp Tomsich */
267bd376714SPhilipp Tomsich
268bd376714SPhilipp Tomsich div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
269bd376714SPhilipp Tomsich return gpll_hz / div;
270bd376714SPhilipp Tomsich }
271bd376714SPhilipp Tomsich
rockchip_spi_probe(struct udevice * bus)2721b2fd5bfSSimon Glass static int rockchip_spi_probe(struct udevice *bus)
2731b2fd5bfSSimon Glass {
2741b2fd5bfSSimon Glass struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
2751b2fd5bfSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
2767d984abeSJon Lin struct rockchip_spi_quirks *quirks_cfg;
2771b2fd5bfSSimon Glass int ret;
2781b2fd5bfSSimon Glass
2791b2fd5bfSSimon Glass debug("%s: probe\n", __func__);
2806e019c4fSSimon Glass #if CONFIG_IS_ENABLED(OF_PLATDATA)
2816e019c4fSSimon Glass ret = conv_of_platdata(bus);
2826e019c4fSSimon Glass if (ret)
2836e019c4fSSimon Glass return ret;
2846e019c4fSSimon Glass #endif
2851b2fd5bfSSimon Glass priv->regs = (struct rockchip_spi *)plat->base;
2861b2fd5bfSSimon Glass
2871b2fd5bfSSimon Glass priv->last_transaction_us = timer_get_us();
2881b2fd5bfSSimon Glass priv->max_freq = plat->frequency;
2891b2fd5bfSSimon Glass
290bd376714SPhilipp Tomsich /* Clamp the value from the DTS against any hardware limits */
291bd376714SPhilipp Tomsich if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE)
292bd376714SPhilipp Tomsich priv->max_freq = ROCKCHIP_SPI_MAX_RATE;
293bd376714SPhilipp Tomsich
294bd376714SPhilipp Tomsich /* Find a module-input clock that fits with the max_freq setting */
295bd376714SPhilipp Tomsich ret = clk_set_rate(&priv->clk,
296bd376714SPhilipp Tomsich rockchip_spi_calc_modclk(priv->max_freq));
2971b2fd5bfSSimon Glass if (ret < 0) {
2981b2fd5bfSSimon Glass debug("%s: Failed to set clock: %d\n", __func__, ret);
2991b2fd5bfSSimon Glass return ret;
3001b2fd5bfSSimon Glass }
3011b2fd5bfSSimon Glass priv->input_rate = ret;
3021b2fd5bfSSimon Glass debug("%s: rate = %u\n", __func__, priv->input_rate);
3031b2fd5bfSSimon Glass priv->bits_per_word = 8;
3047d984abeSJon Lin quirks_cfg = (struct rockchip_spi_quirks *)dev_get_driver_data(bus);
3057d984abeSJon Lin if (quirks_cfg)
3067d984abeSJon Lin priv->max_baud_div_in_cpha = quirks_cfg->max_baud_div_in_cpha;
3071b2fd5bfSSimon Glass
3081b2fd5bfSSimon Glass return 0;
3091b2fd5bfSSimon Glass }
3101b2fd5bfSSimon Glass
rockchip_spi_claim_bus(struct udevice * dev)3111b2fd5bfSSimon Glass static int rockchip_spi_claim_bus(struct udevice *dev)
3121b2fd5bfSSimon Glass {
3131b2fd5bfSSimon Glass struct udevice *bus = dev->parent;
3141b2fd5bfSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
3151b2fd5bfSSimon Glass struct rockchip_spi *regs = priv->regs;
3161b2fd5bfSSimon Glass u8 spi_dfs, spi_tf;
3171b2fd5bfSSimon Glass uint ctrlr0;
3181b2fd5bfSSimon Glass
3191b2fd5bfSSimon Glass /* Disable the SPI hardware */
3201b2fd5bfSSimon Glass rkspi_enable_chip(regs, 0);
3211b2fd5bfSSimon Glass
3221b2fd5bfSSimon Glass switch (priv->bits_per_word) {
3231b2fd5bfSSimon Glass case 8:
3241b2fd5bfSSimon Glass priv->n_bytes = 1;
3251b2fd5bfSSimon Glass spi_dfs = DFS_8BIT;
3261b2fd5bfSSimon Glass spi_tf = HALF_WORD_OFF;
3271b2fd5bfSSimon Glass break;
3281b2fd5bfSSimon Glass case 16:
3291b2fd5bfSSimon Glass priv->n_bytes = 2;
3301b2fd5bfSSimon Glass spi_dfs = DFS_16BIT;
3311b2fd5bfSSimon Glass spi_tf = HALF_WORD_ON;
3321b2fd5bfSSimon Glass break;
3331b2fd5bfSSimon Glass default:
3341b2fd5bfSSimon Glass debug("%s: unsupported bits: %dbits\n", __func__,
3351b2fd5bfSSimon Glass priv->bits_per_word);
3361b2fd5bfSSimon Glass return -EPROTONOSUPPORT;
3371b2fd5bfSSimon Glass }
3381b2fd5bfSSimon Glass
33928a943c1SSimon Glass if (priv->speed_hz != priv->last_speed_hz)
3401b2fd5bfSSimon Glass rkspi_set_clk(priv, priv->speed_hz);
3411b2fd5bfSSimon Glass
3421b2fd5bfSSimon Glass /* Operation Mode */
3431b2fd5bfSSimon Glass ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
3441b2fd5bfSSimon Glass
3451b2fd5bfSSimon Glass /* Data Frame Size */
3462b9fe111SSimon Glass ctrlr0 |= spi_dfs << DFS_SHIFT;
3471b2fd5bfSSimon Glass
3481b2fd5bfSSimon Glass /* set SPI mode 0..3 */
3491b2fd5bfSSimon Glass if (priv->mode & SPI_CPOL)
3501b2fd5bfSSimon Glass ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
3511b2fd5bfSSimon Glass if (priv->mode & SPI_CPHA)
3521b2fd5bfSSimon Glass ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
3531b2fd5bfSSimon Glass
3541b2fd5bfSSimon Glass /* Chip Select Mode */
3551b2fd5bfSSimon Glass ctrlr0 |= CSM_KEEP << CSM_SHIFT;
3561b2fd5bfSSimon Glass
3571b2fd5bfSSimon Glass /* SSN to Sclk_out delay */
3581b2fd5bfSSimon Glass ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
3591b2fd5bfSSimon Glass
3601b2fd5bfSSimon Glass /* Serial Endian Mode */
3611b2fd5bfSSimon Glass ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
3621b2fd5bfSSimon Glass
3631b2fd5bfSSimon Glass /* First Bit Mode */
3641b2fd5bfSSimon Glass ctrlr0 |= FBM_MSB << FBM_SHIFT;
3651b2fd5bfSSimon Glass
3661b2fd5bfSSimon Glass /* Byte and Halfword Transform */
3672b9fe111SSimon Glass ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
3681b2fd5bfSSimon Glass
3691b2fd5bfSSimon Glass /* Rxd Sample Delay */
37064993c87SJon Lin ctrlr0 |= priv->rsd << RXDSD_SHIFT;
3711b2fd5bfSSimon Glass
3721b2fd5bfSSimon Glass /* Frame Format */
3731b2fd5bfSSimon Glass ctrlr0 |= FRF_SPI << FRF_SHIFT;
3741b2fd5bfSSimon Glass
37581a1596cSJon Lin /* Save static configuration */
37681a1596cSJon Lin priv->cr0 = ctrlr0;
3771b2fd5bfSSimon Glass
3781b2fd5bfSSimon Glass writel(ctrlr0, ®s->ctrlr0);
3791b2fd5bfSSimon Glass
3801b2fd5bfSSimon Glass return 0;
3811b2fd5bfSSimon Glass }
3821b2fd5bfSSimon Glass
rockchip_spi_config(struct rockchip_spi_priv * priv,const void * dout)38381a1596cSJon Lin static int rockchip_spi_config(struct rockchip_spi_priv *priv, const void *dout)
38481a1596cSJon Lin {
38581a1596cSJon Lin struct rockchip_spi *regs = priv->regs;
38681a1596cSJon Lin uint ctrlr0 = priv->cr0;
38781a1596cSJon Lin u32 tmod;
38881a1596cSJon Lin
38981a1596cSJon Lin if (dout)
39081a1596cSJon Lin tmod = TMOD_TR;
39181a1596cSJon Lin else
39281a1596cSJon Lin tmod = TMOD_RO;
39381a1596cSJon Lin
39481a1596cSJon Lin ctrlr0 |= (tmod & TMOD_MASK) << TMOD_SHIFT;
39581a1596cSJon Lin writel(ctrlr0, ®s->ctrlr0);
39681a1596cSJon Lin
39781a1596cSJon Lin return 0;
39881a1596cSJon Lin }
39981a1596cSJon Lin
rockchip_spi_release_bus(struct udevice * dev)4001b2fd5bfSSimon Glass static int rockchip_spi_release_bus(struct udevice *dev)
4011b2fd5bfSSimon Glass {
402e15af8e2SSimon Glass struct udevice *bus = dev->parent;
403e15af8e2SSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
404e15af8e2SSimon Glass
405e15af8e2SSimon Glass rkspi_enable_chip(priv->regs, false);
406e15af8e2SSimon Glass
4071b2fd5bfSSimon Glass return 0;
4081b2fd5bfSSimon Glass }
4091b2fd5bfSSimon Glass
rockchip_spi_xfer(struct udevice * dev,unsigned int bitlen,const void * dout,void * din,unsigned long flags)4101b2fd5bfSSimon Glass static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
4111b2fd5bfSSimon Glass const void *dout, void *din, unsigned long flags)
4121b2fd5bfSSimon Glass {
4131b2fd5bfSSimon Glass struct udevice *bus = dev->parent;
4141b2fd5bfSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
4151b2fd5bfSSimon Glass struct rockchip_spi *regs = priv->regs;
4161b2fd5bfSSimon Glass struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
4171b2fd5bfSSimon Glass int len = bitlen >> 3;
4181b2fd5bfSSimon Glass const u8 *out = dout;
4191b2fd5bfSSimon Glass u8 *in = din;
4201b2fd5bfSSimon Glass int toread, towrite;
4211b2fd5bfSSimon Glass int ret;
4221b2fd5bfSSimon Glass
42381a1596cSJon Lin rockchip_spi_config(priv, dout);
42481a1596cSJon Lin
4251b2fd5bfSSimon Glass debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
4261b2fd5bfSSimon Glass len, flags);
4271b2fd5bfSSimon Glass if (DEBUG_RK_SPI)
4281b2fd5bfSSimon Glass rkspi_dump_regs(regs);
4291b2fd5bfSSimon Glass
4301b2fd5bfSSimon Glass /* Assert CS before transfer */
4311b2fd5bfSSimon Glass if (flags & SPI_XFER_BEGIN)
432183a3a0fSSimon Glass spi_cs_activate(dev, slave_plat->cs);
4331b2fd5bfSSimon Glass
4341b2fd5bfSSimon Glass while (len > 0) {
4351b2fd5bfSSimon Glass int todo = min(len, 0xffff);
4361b2fd5bfSSimon Glass
437e15af8e2SSimon Glass rkspi_enable_chip(regs, false);
4381b2fd5bfSSimon Glass writel(todo - 1, ®s->ctrlr1);
4391b2fd5bfSSimon Glass rkspi_enable_chip(regs, true);
4401b2fd5bfSSimon Glass
4411b2fd5bfSSimon Glass toread = todo;
4421b2fd5bfSSimon Glass towrite = todo;
4431b2fd5bfSSimon Glass while (toread || towrite) {
4441b2fd5bfSSimon Glass u32 status = readl(®s->sr);
4451b2fd5bfSSimon Glass
4461b2fd5bfSSimon Glass if (towrite && !(status & SR_TF_FULL)) {
447941111f2SJon Lin if (out)
4481b2fd5bfSSimon Glass writel(out ? *out++ : 0, regs->txdr);
4491b2fd5bfSSimon Glass towrite--;
4501b2fd5bfSSimon Glass }
4511b2fd5bfSSimon Glass if (toread && !(status & SR_RF_EMPT)) {
4521b2fd5bfSSimon Glass u32 byte = readl(regs->rxdr);
4531b2fd5bfSSimon Glass
4541b2fd5bfSSimon Glass if (in)
4551b2fd5bfSSimon Glass *in++ = byte;
4561b2fd5bfSSimon Glass toread--;
4571b2fd5bfSSimon Glass }
4581b2fd5bfSSimon Glass }
4591b2fd5bfSSimon Glass ret = rkspi_wait_till_not_busy(regs);
4601b2fd5bfSSimon Glass if (ret)
4611b2fd5bfSSimon Glass break;
4621b2fd5bfSSimon Glass len -= todo;
4631b2fd5bfSSimon Glass }
4641b2fd5bfSSimon Glass
4651b2fd5bfSSimon Glass /* Deassert CS after transfer */
4661b2fd5bfSSimon Glass if (flags & SPI_XFER_END)
467183a3a0fSSimon Glass spi_cs_deactivate(dev, slave_plat->cs);
4681b2fd5bfSSimon Glass
4691b2fd5bfSSimon Glass rkspi_enable_chip(regs, false);
4701b2fd5bfSSimon Glass
4711b2fd5bfSSimon Glass return ret;
4721b2fd5bfSSimon Glass }
4731b2fd5bfSSimon Glass
rockchip_spi_set_speed(struct udevice * bus,uint speed)4741b2fd5bfSSimon Glass static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
4751b2fd5bfSSimon Glass {
4761b2fd5bfSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
4771b2fd5bfSSimon Glass
478bd376714SPhilipp Tomsich /* Clamp to the maximum frequency specified in the DTS */
4791b2fd5bfSSimon Glass if (speed > priv->max_freq)
4801b2fd5bfSSimon Glass speed = priv->max_freq;
481bd376714SPhilipp Tomsich
4821b2fd5bfSSimon Glass priv->speed_hz = speed;
4831b2fd5bfSSimon Glass
4841b2fd5bfSSimon Glass return 0;
4851b2fd5bfSSimon Glass }
4861b2fd5bfSSimon Glass
rockchip_spi_set_mode(struct udevice * bus,uint mode)4871b2fd5bfSSimon Glass static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
4881b2fd5bfSSimon Glass {
4891b2fd5bfSSimon Glass struct rockchip_spi_priv *priv = dev_get_priv(bus);
4901b2fd5bfSSimon Glass
4911b2fd5bfSSimon Glass priv->mode = mode;
4921b2fd5bfSSimon Glass
4931b2fd5bfSSimon Glass return 0;
4941b2fd5bfSSimon Glass }
4951b2fd5bfSSimon Glass
4961b2fd5bfSSimon Glass static const struct dm_spi_ops rockchip_spi_ops = {
4971b2fd5bfSSimon Glass .claim_bus = rockchip_spi_claim_bus,
4981b2fd5bfSSimon Glass .release_bus = rockchip_spi_release_bus,
4991b2fd5bfSSimon Glass .xfer = rockchip_spi_xfer,
5001b2fd5bfSSimon Glass .set_speed = rockchip_spi_set_speed,
5011b2fd5bfSSimon Glass .set_mode = rockchip_spi_set_mode,
5021b2fd5bfSSimon Glass /*
5031b2fd5bfSSimon Glass * cs_info is not needed, since we require all chip selects to be
5041b2fd5bfSSimon Glass * in the device tree explicitly
5051b2fd5bfSSimon Glass */
5061b2fd5bfSSimon Glass };
5071b2fd5bfSSimon Glass
508*b7cc8342SJon Lin static const struct rockchip_spi_quirks rockchip_spi_quirks_cfg = {
509*b7cc8342SJon Lin .max_baud_div_in_cpha = 4,
510*b7cc8342SJon Lin };
511*b7cc8342SJon Lin
5121b2fd5bfSSimon Glass static const struct udevice_id rockchip_spi_ids[] = {
513*b7cc8342SJon Lin {
514*b7cc8342SJon Lin .compatible = "rockchip,px30-spi",
515*b7cc8342SJon Lin .data = (ulong)&rockchip_spi_quirks_cfg,
516*b7cc8342SJon Lin },
5171b2fd5bfSSimon Glass { .compatible = "rockchip,rk3288-spi" },
518d16120a6SPhilipp Tomsich { .compatible = "rockchip,rk3368-spi" },
519cdeb4d78SJakob Unterwurzacher { .compatible = "rockchip,rk3399-spi" },
52008ab3f3cSYifeng Zhao { .compatible = "rockchip,rk3066-spi" },
52108ab3f3cSYifeng Zhao { .compatible = "rockchip,rk3328-spi" },
5221b2fd5bfSSimon Glass { }
5231b2fd5bfSSimon Glass };
5241b2fd5bfSSimon Glass
5251b2fd5bfSSimon Glass U_BOOT_DRIVER(rockchip_spi) = {
5266e019c4fSSimon Glass #if CONFIG_IS_ENABLED(OF_PLATDATA)
5276e019c4fSSimon Glass .name = "rockchip_rk3288_spi",
5286e019c4fSSimon Glass #else
5291b2fd5bfSSimon Glass .name = "rockchip_spi",
5306e019c4fSSimon Glass #endif
5311b2fd5bfSSimon Glass .id = UCLASS_SPI,
5321b2fd5bfSSimon Glass .of_match = rockchip_spi_ids,
5331b2fd5bfSSimon Glass .ops = &rockchip_spi_ops,
5341b2fd5bfSSimon Glass .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
5351b2fd5bfSSimon Glass .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
5361b2fd5bfSSimon Glass .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
5371b2fd5bfSSimon Glass .probe = rockchip_spi_probe,
5381b2fd5bfSSimon Glass };
539