Skip to content

Commit 5dc349e

Browse files
Chunyan Zhangbroonie
authored andcommitted
spi: sprd: Pass offset instead of physical address to adi_read/_write()
The register offset would be added a physical address base and then pass to the function sprd_adt_read()/_write() each time before calling them. So we can do that within these two functions instead, that would make the code more clear. Signed-off-by: Chunyan Zhang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 745649c commit 5dc349e

File tree

1 file changed

+44
-61
lines changed

1 file changed

+44
-61
lines changed

drivers/spi/spi-sprd-adi.c

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,18 @@ struct sprd_adi {
117117
struct notifier_block restart_handler;
118118
};
119119

120-
static int sprd_adi_check_paddr(struct sprd_adi *sadi, u32 paddr)
120+
static int sprd_adi_check_addr(struct sprd_adi *sadi, u32 reg)
121121
{
122-
if (paddr < sadi->slave_pbase || paddr >
123-
(sadi->slave_pbase + ADI_SLAVE_ADDR_SIZE)) {
122+
if (reg > ADI_SLAVE_ADDR_SIZE) {
124123
dev_err(sadi->dev,
125-
"slave physical address is incorrect, addr = 0x%x\n",
126-
paddr);
124+
"slave address offset is incorrect, reg = 0x%x\n",
125+
reg);
127126
return -EINVAL;
128127
}
129128

130129
return 0;
131130
}
132131

133-
static unsigned long sprd_adi_to_vaddr(struct sprd_adi *sadi, u32 paddr)
134-
{
135-
return (paddr - sadi->slave_pbase + sadi->slave_vbase);
136-
}
137-
138132
static int sprd_adi_drain_fifo(struct sprd_adi *sadi)
139133
{
140134
u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
@@ -161,11 +155,11 @@ static int sprd_adi_fifo_is_full(struct sprd_adi *sadi)
161155
return readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS) & BIT_FIFO_FULL;
162156
}
163157

164-
static int sprd_adi_read(struct sprd_adi *sadi, u32 reg_paddr, u32 *read_val)
158+
static int sprd_adi_read(struct sprd_adi *sadi, u32 reg, u32 *read_val)
165159
{
166160
int read_timeout = ADI_READ_TIMEOUT;
167161
unsigned long flags;
168-
u32 val, rd_addr;
162+
u32 val, rd_addr, paddr;
169163
int ret = 0;
170164

171165
if (sadi->hwlock) {
@@ -178,11 +172,16 @@ static int sprd_adi_read(struct sprd_adi *sadi, u32 reg_paddr, u32 *read_val)
178172
}
179173
}
180174

175+
ret = sprd_adi_check_addr(sadi, reg);
176+
if (ret)
177+
goto out;
178+
181179
/*
182180
* Set the physical register address need to read into RD_CMD register,
183181
* then ADI controller will start to transfer automatically.
184182
*/
185-
writel_relaxed(reg_paddr, sadi->base + REG_ADI_RD_CMD);
183+
paddr = sadi->slave_pbase + reg;
184+
writel_relaxed(paddr, sadi->base + REG_ADI_RD_CMD);
186185

187186
/*
188187
* Wait read operation complete, the BIT_RD_CMD_BUSY will be set
@@ -212,9 +211,9 @@ static int sprd_adi_read(struct sprd_adi *sadi, u32 reg_paddr, u32 *read_val)
212211
*/
213212
rd_addr = (val & RD_ADDR_MASK) >> RD_ADDR_SHIFT;
214213

215-
if (rd_addr != (reg_paddr & REG_ADDR_LOW_MASK)) {
214+
if (rd_addr != (paddr & REG_ADDR_LOW_MASK)) {
216215
dev_err(sadi->dev, "read error, reg addr = 0x%x, val = 0x%x\n",
217-
reg_paddr, val);
216+
paddr, val);
218217
ret = -EIO;
219218
goto out;
220219
}
@@ -227,9 +226,8 @@ static int sprd_adi_read(struct sprd_adi *sadi, u32 reg_paddr, u32 *read_val)
227226
return ret;
228227
}
229228

230-
static int sprd_adi_write(struct sprd_adi *sadi, u32 reg_paddr, u32 val)
229+
static int sprd_adi_write(struct sprd_adi *sadi, u32 reg, u32 val)
231230
{
232-
unsigned long reg = sprd_adi_to_vaddr(sadi, reg_paddr);
233231
u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
234232
unsigned long flags;
235233
int ret;
@@ -244,6 +242,10 @@ static int sprd_adi_write(struct sprd_adi *sadi, u32 reg_paddr, u32 val)
244242
}
245243
}
246244

245+
ret = sprd_adi_check_addr(sadi, reg);
246+
if (ret)
247+
goto out;
248+
247249
ret = sprd_adi_drain_fifo(sadi);
248250
if (ret < 0)
249251
goto out;
@@ -254,7 +256,8 @@ static int sprd_adi_write(struct sprd_adi *sadi, u32 reg_paddr, u32 val)
254256
*/
255257
do {
256258
if (!sprd_adi_fifo_is_full(sadi)) {
257-
writel_relaxed(val, (void __iomem *)reg);
259+
/* we need virtual register address to write. */
260+
writel_relaxed(val, (void __iomem *)(sadi->slave_vbase + reg));
258261
break;
259262
}
260263

@@ -277,44 +280,24 @@ static int sprd_adi_transfer_one(struct spi_controller *ctlr,
277280
struct spi_transfer *t)
278281
{
279282
struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
280-
u32 phy_reg, val;
283+
u32 reg, val;
281284
int ret;
282285

283286
if (t->rx_buf) {
284-
phy_reg = *(u32 *)t->rx_buf + sadi->slave_pbase;
285-
286-
ret = sprd_adi_check_paddr(sadi, phy_reg);
287-
if (ret)
288-
return ret;
289-
290-
ret = sprd_adi_read(sadi, phy_reg, &val);
291-
if (ret)
292-
return ret;
293-
287+
reg = *(u32 *)t->rx_buf;
288+
ret = sprd_adi_read(sadi, reg, &val);
294289
*(u32 *)t->rx_buf = val;
295290
} else if (t->tx_buf) {
296291
u32 *p = (u32 *)t->tx_buf;
297-
298-
/*
299-
* Get the physical register address need to write and convert
300-
* the physical address to virtual address. Since we need
301-
* virtual register address to write.
302-
*/
303-
phy_reg = *p++ + sadi->slave_pbase;
304-
ret = sprd_adi_check_paddr(sadi, phy_reg);
305-
if (ret)
306-
return ret;
307-
292+
reg = *p++;
308293
val = *p;
309-
ret = sprd_adi_write(sadi, phy_reg, val);
310-
if (ret)
311-
return ret;
294+
ret = sprd_adi_write(sadi, reg, val);
312295
} else {
313296
dev_err(sadi->dev, "no buffer for transfer\n");
314-
return -EINVAL;
297+
ret = -EINVAL;
315298
}
316299

317-
return 0;
300+
return ret;
318301
}
319302

320303
static void sprd_adi_set_wdt_rst_mode(struct sprd_adi *sadi)
@@ -323,9 +306,9 @@ static void sprd_adi_set_wdt_rst_mode(struct sprd_adi *sadi)
323306
u32 val;
324307

325308
/* Set default watchdog reboot mode */
326-
sprd_adi_read(sadi, sadi->slave_pbase + PMIC_RST_STATUS, &val);
309+
sprd_adi_read(sadi, PMIC_RST_STATUS, &val);
327310
val |= HWRST_STATUS_WATCHDOG;
328-
sprd_adi_write(sadi, sadi->slave_pbase + PMIC_RST_STATUS, val);
311+
sprd_adi_write(sadi, PMIC_RST_STATUS, val);
329312
#endif
330313
}
331314

@@ -366,40 +349,40 @@ static int sprd_adi_restart_handler(struct notifier_block *this,
366349
reboot_mode = HWRST_STATUS_NORMAL;
367350

368351
/* Record the reboot mode */
369-
sprd_adi_read(sadi, sadi->slave_pbase + PMIC_RST_STATUS, &val);
352+
sprd_adi_read(sadi, PMIC_RST_STATUS, &val);
370353
val &= ~HWRST_STATUS_WATCHDOG;
371354
val |= reboot_mode;
372-
sprd_adi_write(sadi, sadi->slave_pbase + PMIC_RST_STATUS, val);
355+
sprd_adi_write(sadi, PMIC_RST_STATUS, val);
373356

374357
/* Enable the interface clock of the watchdog */
375-
sprd_adi_read(sadi, sadi->slave_pbase + PMIC_MODULE_EN, &val);
358+
sprd_adi_read(sadi, PMIC_MODULE_EN, &val);
376359
val |= BIT_WDG_EN;
377-
sprd_adi_write(sadi, sadi->slave_pbase + PMIC_MODULE_EN, val);
360+
sprd_adi_write(sadi, PMIC_MODULE_EN, val);
378361

379362
/* Enable the work clock of the watchdog */
380-
sprd_adi_read(sadi, sadi->slave_pbase + PMIC_CLK_EN, &val);
363+
sprd_adi_read(sadi, PMIC_CLK_EN, &val);
381364
val |= BIT_WDG_EN;
382-
sprd_adi_write(sadi, sadi->slave_pbase + PMIC_CLK_EN, val);
365+
sprd_adi_write(sadi, PMIC_CLK_EN, val);
383366

384367
/* Unlock the watchdog */
385-
sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOCK, WDG_UNLOCK_KEY);
368+
sprd_adi_write(sadi, REG_WDG_LOCK, WDG_UNLOCK_KEY);
386369

387-
sprd_adi_read(sadi, sadi->slave_pbase + REG_WDG_CTRL, &val);
370+
sprd_adi_read(sadi, REG_WDG_CTRL, &val);
388371
val |= BIT_WDG_NEW;
389-
sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_CTRL, val);
372+
sprd_adi_write(sadi, REG_WDG_CTRL, val);
390373

391374
/* Load the watchdog timeout value, 50ms is always enough. */
392-
sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOAD_HIGH, 0);
393-
sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOAD_LOW,
375+
sprd_adi_write(sadi, REG_WDG_LOAD_HIGH, 0);
376+
sprd_adi_write(sadi, REG_WDG_LOAD_LOW,
394377
WDG_LOAD_VAL & WDG_LOAD_MASK);
395378

396379
/* Start the watchdog to reset system */
397-
sprd_adi_read(sadi, sadi->slave_pbase + REG_WDG_CTRL, &val);
380+
sprd_adi_read(sadi, REG_WDG_CTRL, &val);
398381
val |= BIT_WDG_RUN | BIT_WDG_RST;
399-
sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_CTRL, val);
382+
sprd_adi_write(sadi, REG_WDG_CTRL, val);
400383

401384
/* Lock the watchdog */
402-
sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOCK, ~WDG_UNLOCK_KEY);
385+
sprd_adi_write(sadi, REG_WDG_LOCK, ~WDG_UNLOCK_KEY);
403386

404387
mdelay(1000);
405388

0 commit comments

Comments
 (0)