Skip to content

Commit b64836a

Browse files
larsclausenbroonie
authored andcommitted
spi: altera: Consolidate TX/RX data register access
The patterns for accessing the TX/RX data registers is the same for the IRQ and non-IRQ paths. Consolidate the duplicated code into shared helper functions. Signed-off-by: Lars-Peter Clausen <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent e19b63c commit b64836a

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

drivers/spi/spi-altera.c

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,43 @@ static void altera_spi_set_cs(struct spi_device *spi, bool is_high)
7676
}
7777
}
7878

79-
static inline unsigned int hw_txbyte(struct altera_spi *hw, int count)
79+
static void altera_spi_tx_word(struct altera_spi *hw)
8080
{
81+
unsigned int txd = 0;
82+
8183
if (hw->tx) {
8284
switch (hw->bytes_per_word) {
8385
case 1:
84-
return hw->tx[count];
86+
txd = hw->tx[hw->count];
87+
break;
8588
case 2:
86-
return (hw->tx[count * 2]
87-
| (hw->tx[count * 2 + 1] << 8));
89+
txd = (hw->tx[hw->count * 2]
90+
| (hw->tx[hw->count * 2 + 1] << 8));
91+
break;
8892
}
8993
}
90-
return 0;
94+
95+
writel(txd, hw->base + ALTERA_SPI_TXDATA);
96+
}
97+
98+
static void altera_spi_rx_word(struct altera_spi *hw)
99+
{
100+
unsigned int rxd;
101+
102+
rxd = readl(hw->base + ALTERA_SPI_RXDATA);
103+
if (hw->rx) {
104+
switch (hw->bytes_per_word) {
105+
case 1:
106+
hw->rx[hw->count] = rxd;
107+
break;
108+
case 2:
109+
hw->rx[hw->count * 2] = rxd;
110+
hw->rx[hw->count * 2 + 1] = rxd >> 8;
111+
break;
112+
}
113+
}
114+
115+
hw->count++;
91116
}
92117

93118
static int altera_spi_txrx(struct spi_master *master,
@@ -107,32 +132,16 @@ static int altera_spi_txrx(struct spi_master *master,
107132
writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
108133

109134
/* send the first byte */
110-
writel(hw_txbyte(hw, 0), hw->base + ALTERA_SPI_TXDATA);
135+
altera_spi_tx_word(hw);
111136
} else {
112137
while (hw->count < hw->len) {
113-
unsigned int rxd;
114-
115-
writel(hw_txbyte(hw, hw->count),
116-
hw->base + ALTERA_SPI_TXDATA);
138+
altera_spi_tx_word(hw);
117139

118140
while (!(readl(hw->base + ALTERA_SPI_STATUS) &
119141
ALTERA_SPI_STATUS_RRDY_MSK))
120142
cpu_relax();
121143

122-
rxd = readl(hw->base + ALTERA_SPI_RXDATA);
123-
if (hw->rx) {
124-
switch (hw->bytes_per_word) {
125-
case 1:
126-
hw->rx[hw->count] = rxd;
127-
break;
128-
case 2:
129-
hw->rx[hw->count * 2] = rxd;
130-
hw->rx[hw->count * 2 + 1] = rxd >> 8;
131-
break;
132-
}
133-
}
134-
135-
hw->count++;
144+
altera_spi_rx_word(hw);
136145
}
137146
spi_finalize_current_transfer(master);
138147
}
@@ -144,25 +153,11 @@ static irqreturn_t altera_spi_irq(int irq, void *dev)
144153
{
145154
struct spi_master *master = dev;
146155
struct altera_spi *hw = spi_master_get_devdata(master);
147-
unsigned int rxd;
148-
149-
rxd = readl(hw->base + ALTERA_SPI_RXDATA);
150-
if (hw->rx) {
151-
switch (hw->bytes_per_word) {
152-
case 1:
153-
hw->rx[hw->count] = rxd;
154-
break;
155-
case 2:
156-
hw->rx[hw->count * 2] = rxd;
157-
hw->rx[hw->count * 2 + 1] = rxd >> 8;
158-
break;
159-
}
160-
}
161156

162-
hw->count++;
157+
altera_spi_rx_word(hw);
163158

164159
if (hw->count < hw->len) {
165-
writel(hw_txbyte(hw, hw->count), hw->base + ALTERA_SPI_TXDATA);
160+
altera_spi_tx_word(hw);
166161
} else {
167162
/* disable receive interrupt */
168163
hw->imr &= ~ALTERA_SPI_CONTROL_IRRDY_MSK;

0 commit comments

Comments
 (0)