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