Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com> |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 85d6531 | 2019-12-28 10:44:58 -0700 | [diff] [blame] | 6 | #include <clock_legacy.h> |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 7 | #include <spi.h> |
| 8 | #include <dm.h> |
| 9 | #include <div64.h> |
| 10 | #include <errno.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 11 | #include <time.h> |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/addrspace.h> |
| 14 | #include <asm/types.h> |
| 15 | #include <dm/pinctrl.h> |
| 16 | #include <mach/ar71xx_regs.h> |
| 17 | |
| 18 | /* CLOCK_DIVIDER = 3 (SPI clock = 200 / 8 ~ 25 MHz) */ |
| 19 | #define ATH79_SPI_CLK_DIV(x) (((x) >> 1) - 1) |
| 20 | #define ATH79_SPI_RRW_DELAY_FACTOR 12000 |
| 21 | #define ATH79_SPI_MHZ (1000 * 1000) |
| 22 | |
| 23 | struct ath79_spi_priv { |
| 24 | void __iomem *regs; |
| 25 | u32 rrw_delay; |
| 26 | }; |
| 27 | |
| 28 | static void spi_cs_activate(struct udevice *dev) |
| 29 | { |
| 30 | struct udevice *bus = dev_get_parent(dev); |
| 31 | struct ath79_spi_priv *priv = dev_get_priv(bus); |
| 32 | |
| 33 | writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS); |
| 34 | writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC); |
| 35 | } |
| 36 | |
| 37 | static void spi_cs_deactivate(struct udevice *dev) |
| 38 | { |
| 39 | struct udevice *bus = dev_get_parent(dev); |
| 40 | struct ath79_spi_priv *priv = dev_get_priv(bus); |
| 41 | |
| 42 | writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC); |
| 43 | writel(0, priv->regs + AR71XX_SPI_REG_FS); |
| 44 | } |
| 45 | |
| 46 | static int ath79_spi_claim_bus(struct udevice *dev) |
| 47 | { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int ath79_spi_release_bus(struct udevice *dev) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int ath79_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 57 | const void *dout, void *din, unsigned long flags) |
| 58 | { |
| 59 | struct udevice *bus = dev_get_parent(dev); |
| 60 | struct ath79_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 61 | struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev); |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 62 | u8 *rx = din; |
| 63 | const u8 *tx = dout; |
| 64 | u8 curbyte, curbitlen, restbits; |
| 65 | u32 bytes = bitlen / 8; |
| 66 | u32 out, in; |
| 67 | u64 tick; |
| 68 | |
| 69 | if (flags & SPI_XFER_BEGIN) |
| 70 | spi_cs_activate(dev); |
| 71 | |
| 72 | restbits = (bitlen % 8); |
| 73 | if (restbits) |
| 74 | bytes++; |
| 75 | |
| 76 | out = AR71XX_SPI_IOC_CS_ALL & ~(AR71XX_SPI_IOC_CS(slave->cs)); |
| 77 | while (bytes > 0) { |
| 78 | bytes--; |
| 79 | curbyte = 0; |
| 80 | if (tx) |
| 81 | curbyte = *tx++; |
| 82 | |
| 83 | if (restbits && !bytes) { |
| 84 | curbitlen = restbits; |
| 85 | curbyte <<= 8 - restbits; |
| 86 | } else { |
| 87 | curbitlen = 8; |
| 88 | } |
| 89 | |
| 90 | for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) { |
| 91 | if (curbyte & 0x80) |
| 92 | out |= AR71XX_SPI_IOC_DO; |
| 93 | else |
| 94 | out &= ~(AR71XX_SPI_IOC_DO); |
| 95 | |
| 96 | writel(out, priv->regs + AR71XX_SPI_REG_IOC); |
| 97 | |
| 98 | /* delay for low level */ |
| 99 | if (priv->rrw_delay) { |
| 100 | tick = get_ticks() + priv->rrw_delay; |
| 101 | while (get_ticks() < tick) |
| 102 | /*NOP*/; |
| 103 | } |
| 104 | |
| 105 | writel(out | AR71XX_SPI_IOC_CLK, |
| 106 | priv->regs + AR71XX_SPI_REG_IOC); |
| 107 | |
| 108 | /* delay for high level */ |
| 109 | if (priv->rrw_delay) { |
| 110 | tick = get_ticks() + priv->rrw_delay; |
| 111 | while (get_ticks() < tick) |
| 112 | /*NOP*/; |
| 113 | } |
| 114 | |
| 115 | curbyte <<= 1; |
| 116 | } |
| 117 | |
| 118 | if (!bytes) |
| 119 | writel(out, priv->regs + AR71XX_SPI_REG_IOC); |
| 120 | |
| 121 | in = readl(priv->regs + AR71XX_SPI_REG_RDS); |
| 122 | if (rx) { |
| 123 | if (restbits && !bytes) |
| 124 | *rx++ = (in << (8 - restbits)); |
| 125 | else |
| 126 | *rx++ = in; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (flags & SPI_XFER_END) |
| 131 | spi_cs_deactivate(dev); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 136 | static int ath79_spi_set_speed(struct udevice *bus, uint speed) |
| 137 | { |
| 138 | struct ath79_spi_priv *priv = dev_get_priv(bus); |
| 139 | u32 val, div = 0; |
| 140 | u64 time; |
| 141 | |
| 142 | if (speed) |
| 143 | div = get_bus_freq(0) / speed; |
| 144 | |
| 145 | if (div > 63) |
| 146 | div = 63; |
| 147 | |
| 148 | if (div < 5) |
| 149 | div = 5; |
| 150 | |
| 151 | /* calculate delay */ |
| 152 | time = get_tbclk(); |
| 153 | do_div(time, speed / 2); |
| 154 | val = get_bus_freq(0) / ATH79_SPI_MHZ; |
| 155 | val = ATH79_SPI_RRW_DELAY_FACTOR / val; |
| 156 | if (time > val) |
| 157 | priv->rrw_delay = time - val + 1; |
| 158 | else |
| 159 | priv->rrw_delay = 0; |
| 160 | |
| 161 | writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS); |
| 162 | clrsetbits_be32(priv->regs + AR71XX_SPI_REG_CTRL, |
| 163 | AR71XX_SPI_CTRL_DIV_MASK, |
| 164 | ATH79_SPI_CLK_DIV(div)); |
| 165 | writel(0, priv->regs + AR71XX_SPI_REG_FS); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int ath79_spi_set_mode(struct udevice *bus, uint mode) |
| 170 | { |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int ath79_spi_probe(struct udevice *bus) |
| 175 | { |
| 176 | struct ath79_spi_priv *priv = dev_get_priv(bus); |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 177 | fdt_addr_t addr; |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 178 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 179 | addr = dev_read_addr(bus); |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 180 | if (addr == FDT_ADDR_T_NONE) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | priv->regs = map_physmem(addr, |
| 184 | AR71XX_SPI_SIZE, |
| 185 | MAP_NOCACHE); |
| 186 | |
| 187 | /* Init SPI Hardware, disable remap, set clock */ |
| 188 | writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS); |
| 189 | writel(AR71XX_SPI_CTRL_RD | ATH79_SPI_CLK_DIV(8), |
| 190 | priv->regs + AR71XX_SPI_REG_CTRL); |
| 191 | writel(0, priv->regs + AR71XX_SPI_REG_FS); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int ath79_cs_info(struct udevice *bus, uint cs, |
| 197 | struct spi_cs_info *info) |
| 198 | { |
| 199 | /* Always allow activity on CS 0/1/2 */ |
| 200 | if (cs >= 3) |
Bin Meng | f8586f6 | 2019-09-09 06:00:01 -0700 | [diff] [blame] | 201 | return -EINVAL; |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static const struct dm_spi_ops ath79_spi_ops = { |
| 207 | .claim_bus = ath79_spi_claim_bus, |
| 208 | .release_bus = ath79_spi_release_bus, |
| 209 | .xfer = ath79_spi_xfer, |
| 210 | .set_speed = ath79_spi_set_speed, |
| 211 | .set_mode = ath79_spi_set_mode, |
| 212 | .cs_info = ath79_cs_info, |
| 213 | }; |
| 214 | |
| 215 | static const struct udevice_id ath79_spi_ids[] = { |
| 216 | { .compatible = "qca,ar7100-spi" }, |
| 217 | {} |
| 218 | }; |
| 219 | |
| 220 | U_BOOT_DRIVER(ath79_spi) = { |
| 221 | .name = "ath79_spi", |
| 222 | .id = UCLASS_SPI, |
| 223 | .of_match = ath79_spi_ids, |
| 224 | .ops = &ath79_spi_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 225 | .priv_auto = sizeof(struct ath79_spi_priv), |
Wills Wang | f502148 | 2016-03-16 16:59:58 +0800 | [diff] [blame] | 226 | .probe = ath79_spi_probe, |
| 227 | }; |