Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Altera SPI driver |
| 4 | * |
| 5 | * based on bfin_spi.c |
| 6 | * Copyright (c) 2005-2008 Analog Devices Inc. |
| 7 | * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 12 | #include <malloc.h> |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 13 | #include <fdtdec.h> |
Jagan Teki | bef87ad | 2015-10-27 23:11:11 +0530 | [diff] [blame] | 14 | #include <spi.h> |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | |
Jagan Teki | bef87ad | 2015-10-27 23:11:11 +0530 | [diff] [blame] | 17 | #define ALTERA_SPI_STATUS_RRDY_MSK BIT(7) |
| 18 | #define ALTERA_SPI_CONTROL_SSO_MSK BIT(10) |
| 19 | |
Marek Vasut | cdcdad8 | 2014-10-22 21:56:04 +0200 | [diff] [blame] | 20 | #ifndef CONFIG_ALTERA_SPI_IDLE_VAL |
Jagan Teki | bef87ad | 2015-10-27 23:11:11 +0530 | [diff] [blame] | 21 | #define CONFIG_ALTERA_SPI_IDLE_VAL 0xff |
Marek Vasut | cdcdad8 | 2014-10-22 21:56:04 +0200 | [diff] [blame] | 22 | #endif |
| 23 | |
Marek Vasut | eef6702 | 2014-10-22 21:55:58 +0200 | [diff] [blame] | 24 | struct altera_spi_regs { |
| 25 | u32 rxdata; |
| 26 | u32 txdata; |
| 27 | u32 status; |
| 28 | u32 control; |
| 29 | u32 _reserved; |
| 30 | u32 slave_sel; |
| 31 | }; |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 32 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 33 | struct altera_spi_platdata { |
| 34 | struct altera_spi_regs *regs; |
| 35 | }; |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 36 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 37 | struct altera_spi_priv { |
| 38 | struct altera_spi_regs *regs; |
| 39 | }; |
| 40 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 41 | static void spi_cs_activate(struct udevice *dev, uint cs) |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 42 | { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 43 | struct udevice *bus = dev->parent; |
| 44 | struct altera_spi_priv *priv = dev_get_priv(bus); |
| 45 | struct altera_spi_regs *const regs = priv->regs; |
| 46 | |
| 47 | writel(1 << cs, ®s->slave_sel); |
| 48 | writel(ALTERA_SPI_CONTROL_SSO_MSK, ®s->control); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 51 | static void spi_cs_deactivate(struct udevice *dev) |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 52 | { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 53 | struct udevice *bus = dev->parent; |
| 54 | struct altera_spi_priv *priv = dev_get_priv(bus); |
| 55 | struct altera_spi_regs *const regs = priv->regs; |
| 56 | |
| 57 | writel(0, ®s->control); |
| 58 | writel(0, ®s->slave_sel); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 61 | static int altera_spi_claim_bus(struct udevice *dev) |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 62 | { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 63 | struct udevice *bus = dev->parent; |
| 64 | struct altera_spi_priv *priv = dev_get_priv(bus); |
| 65 | struct altera_spi_regs *const regs = priv->regs; |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 66 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 67 | writel(0, ®s->control); |
| 68 | writel(0, ®s->slave_sel); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 69 | |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 73 | static int altera_spi_release_bus(struct udevice *dev) |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 74 | { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 75 | struct udevice *bus = dev->parent; |
| 76 | struct altera_spi_priv *priv = dev_get_priv(bus); |
| 77 | struct altera_spi_regs *const regs = priv->regs; |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 78 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 79 | writel(0, ®s->slave_sel); |
| 80 | |
| 81 | return 0; |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 84 | static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 85 | const void *dout, void *din, unsigned long flags) |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 86 | { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 87 | struct udevice *bus = dev->parent; |
| 88 | struct altera_spi_priv *priv = dev_get_priv(bus); |
| 89 | struct altera_spi_regs *const regs = priv->regs; |
| 90 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 91 | |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 92 | /* assume spi core configured to do 8 bit transfers */ |
Marek Vasut | bc76b82 | 2014-10-22 21:56:02 +0200 | [diff] [blame] | 93 | unsigned int bytes = bitlen / 8; |
| 94 | const unsigned char *txp = dout; |
| 95 | unsigned char *rxp = din; |
| 96 | uint32_t reg, data, start; |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 97 | |
| 98 | debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__, |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 99 | bus->seq, slave_plat->cs, bitlen, bytes, flags); |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 100 | |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 101 | if (bitlen == 0) |
| 102 | goto done; |
| 103 | |
| 104 | if (bitlen % 8) { |
| 105 | flags |= SPI_XFER_END; |
| 106 | goto done; |
| 107 | } |
| 108 | |
| 109 | /* empty read buffer */ |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 110 | if (readl(®s->status) & ALTERA_SPI_STATUS_RRDY_MSK) |
| 111 | readl(®s->rxdata); |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 112 | |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 113 | if (flags & SPI_XFER_BEGIN) |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 114 | spi_cs_activate(dev, slave_plat->cs); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 115 | |
| 116 | while (bytes--) { |
Marek Vasut | bc76b82 | 2014-10-22 21:56:02 +0200 | [diff] [blame] | 117 | if (txp) |
| 118 | data = *txp++; |
| 119 | else |
| 120 | data = CONFIG_ALTERA_SPI_IDLE_VAL; |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 121 | |
Marek Vasut | bc76b82 | 2014-10-22 21:56:02 +0200 | [diff] [blame] | 122 | debug("%s: tx:%x ", __func__, data); |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 123 | writel(data, ®s->txdata); |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 124 | |
Marek Vasut | 80d7333 | 2014-10-22 21:56:01 +0200 | [diff] [blame] | 125 | start = get_timer(0); |
| 126 | while (1) { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 127 | reg = readl(®s->status); |
Marek Vasut | 80d7333 | 2014-10-22 21:56:01 +0200 | [diff] [blame] | 128 | if (reg & ALTERA_SPI_STATUS_RRDY_MSK) |
| 129 | break; |
| 130 | if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) { |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 131 | debug("%s: Transmission timed out!\n", __func__); |
| 132 | return -1; |
Marek Vasut | 80d7333 | 2014-10-22 21:56:01 +0200 | [diff] [blame] | 133 | } |
| 134 | } |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 135 | |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 136 | data = readl(®s->rxdata); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 137 | if (rxp) |
Marek Vasut | bc76b82 | 2014-10-22 21:56:02 +0200 | [diff] [blame] | 138 | *rxp++ = data & 0xff; |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 139 | |
Marek Vasut | bc76b82 | 2014-10-22 21:56:02 +0200 | [diff] [blame] | 140 | debug("rx:%x\n", data); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 141 | } |
Marek Vasut | 37dcc13 | 2014-10-22 21:56:00 +0200 | [diff] [blame] | 142 | |
| 143 | done: |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 144 | if (flags & SPI_XFER_END) |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 145 | spi_cs_deactivate(dev); |
Thomas Chou | 661ba14 | 2010-04-30 11:34:16 +0800 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 149 | |
| 150 | static int altera_spi_set_speed(struct udevice *bus, uint speed) |
| 151 | { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int altera_spi_set_mode(struct udevice *bus, uint mode) |
| 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int altera_spi_probe(struct udevice *bus) |
| 161 | { |
| 162 | struct altera_spi_platdata *plat = dev_get_platdata(bus); |
| 163 | struct altera_spi_priv *priv = dev_get_priv(bus); |
| 164 | |
| 165 | priv->regs = plat->regs; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int altera_spi_ofdata_to_platdata(struct udevice *bus) |
| 171 | { |
| 172 | struct altera_spi_platdata *plat = dev_get_platdata(bus); |
| 173 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 174 | plat->regs = map_physmem(devfdt_get_addr(bus), |
Thomas Chou | 7313e21 | 2015-11-14 11:17:25 +0800 | [diff] [blame] | 175 | sizeof(struct altera_spi_regs), |
| 176 | MAP_NOCACHE); |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static const struct dm_spi_ops altera_spi_ops = { |
| 182 | .claim_bus = altera_spi_claim_bus, |
| 183 | .release_bus = altera_spi_release_bus, |
| 184 | .xfer = altera_spi_xfer, |
| 185 | .set_speed = altera_spi_set_speed, |
| 186 | .set_mode = altera_spi_set_mode, |
| 187 | /* |
| 188 | * cs_info is not needed, since we require all chip selects to be |
| 189 | * in the device tree explicitly |
| 190 | */ |
| 191 | }; |
| 192 | |
| 193 | static const struct udevice_id altera_spi_ids[] = { |
Thomas Chou | ddf34c2 | 2015-10-31 20:55:48 +0800 | [diff] [blame] | 194 | { .compatible = "altr,spi-1.0" }, |
| 195 | {} |
Thomas Chou | 15a56f9 | 2015-10-14 08:33:34 +0800 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | U_BOOT_DRIVER(altera_spi) = { |
| 199 | .name = "altera_spi", |
| 200 | .id = UCLASS_SPI, |
| 201 | .of_match = altera_spi_ids, |
| 202 | .ops = &altera_spi_ops, |
| 203 | .ofdata_to_platdata = altera_spi_ofdata_to_platdata, |
| 204 | .platdata_auto_alloc_size = sizeof(struct altera_spi_platdata), |
| 205 | .priv_auto_alloc_size = sizeof(struct altera_spi_priv), |
| 206 | .probe = altera_spi_probe, |
| 207 | }; |