blob: 2b844594a9e6dfda364aec5db3f962c07d00d809 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * SPI_PPC4XX SPI controller driver.
4 *
5 * Copyright (C) 2007 Gary Jennejohn <garyj@denx.de>
6 * Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
7 * Copyright 2009 Harris Corporation, Steven A. Falco <sfalco@harris.com>
8 *
9 * Based in part on drivers/spi/spi_s3c24xx.c
10 *
11 * Copyright (c) 2006 Ben Dooks
12 * Copyright (c) 2006 Simtec Electronics
13 * Ben Dooks <ben@simtec.co.uk>
14 */
15
16/*
17 * The PPC4xx SPI controller has no FIFO so each sent/received byte will
18 * generate an interrupt to the CPU. This can cause high CPU utilization.
19 * This driver allows platforms to reduce the interrupt load on the CPU
20 * during SPI transfers by setting max_speed_hz via the device tree.
21 */
22
23#include <linux/module.h>
24#include <linux/sched.h>
25#include <linux/slab.h>
26#include <linux/errno.h>
27#include <linux/wait.h>
28#include <linux/of_address.h>
29#include <linux/of_platform.h>
30#include <linux/of_gpio.h>
31#include <linux/interrupt.h>
32#include <linux/delay.h>
33
34#include <linux/gpio.h>
35#include <linux/spi/spi.h>
36#include <linux/spi/spi_bitbang.h>
37
38#include <asm/io.h>
39#include <asm/dcr.h>
40#include <asm/dcr-regs.h>
41
42/* bits in mode register - bit 0 is MSb */
43
44/*
45 * SPI_PPC4XX_MODE_SCP = 0 means "data latched on trailing edge of clock"
46 * SPI_PPC4XX_MODE_SCP = 1 means "data latched on leading edge of clock"
47 * Note: This is the inverse of CPHA.
48 */
49#define SPI_PPC4XX_MODE_SCP (0x80 >> 3)
50
51/* SPI_PPC4XX_MODE_SPE = 1 means "port enabled" */
52#define SPI_PPC4XX_MODE_SPE (0x80 >> 4)
53
54/*
55 * SPI_PPC4XX_MODE_RD = 0 means "MSB first" - this is the normal mode
56 * SPI_PPC4XX_MODE_RD = 1 means "LSB first" - this is bit-reversed mode
57 * Note: This is identical to SPI_LSB_FIRST.
58 */
59#define SPI_PPC4XX_MODE_RD (0x80 >> 5)
60
61/*
62 * SPI_PPC4XX_MODE_CI = 0 means "clock idles low"
63 * SPI_PPC4XX_MODE_CI = 1 means "clock idles high"
64 * Note: This is identical to CPOL.
65 */
66#define SPI_PPC4XX_MODE_CI (0x80 >> 6)
67
68/*
69 * SPI_PPC4XX_MODE_IL = 0 means "loopback disable"
70 * SPI_PPC4XX_MODE_IL = 1 means "loopback enable"
71 */
72#define SPI_PPC4XX_MODE_IL (0x80 >> 7)
73
74/* bits in control register */
75/* starts a transfer when set */
76#define SPI_PPC4XX_CR_STR (0x80 >> 7)
77
78/* bits in status register */
79/* port is busy with a transfer */
80#define SPI_PPC4XX_SR_BSY (0x80 >> 6)
81/* RxD ready */
82#define SPI_PPC4XX_SR_RBR (0x80 >> 7)
83
84/* clock settings (SCP and CI) for various SPI modes */
85#define SPI_CLK_MODE0 (SPI_PPC4XX_MODE_SCP | 0)
86#define SPI_CLK_MODE1 (0 | 0)
87#define SPI_CLK_MODE2 (SPI_PPC4XX_MODE_SCP | SPI_PPC4XX_MODE_CI)
88#define SPI_CLK_MODE3 (0 | SPI_PPC4XX_MODE_CI)
89
90#define DRIVER_NAME "spi_ppc4xx_of"
91
92struct spi_ppc4xx_regs {
93 u8 mode;
94 u8 rxd;
95 u8 txd;
96 u8 cr;
97 u8 sr;
98 u8 dummy;
99 /*
100 * Clock divisor modulus register
101 * This uses the following formula:
102 * SCPClkOut = OPBCLK/(4(CDM + 1))
103 * or
104 * CDM = (OPBCLK/4*SCPClkOut) - 1
105 * bit 0 is the MSb!
106 */
107 u8 cdm;
108};
109
110/* SPI Controller driver's private data. */
111struct ppc4xx_spi {
112 /* bitbang has to be first */
113 struct spi_bitbang bitbang;
114 struct completion done;
115
116 u64 mapbase;
117 u64 mapsize;
118 int irqnum;
119 /* need this to set the SPI clock */
120 unsigned int opb_freq;
121
122 /* for transfers */
123 int len;
124 int count;
125 /* data buffers */
126 const unsigned char *tx;
127 unsigned char *rx;
128
129 int *gpios;
130
131 struct spi_ppc4xx_regs __iomem *regs; /* pointer to the registers */
132 struct spi_master *master;
133 struct device *dev;
134};
135
136/* need this so we can set the clock in the chipselect routine */
137struct spi_ppc4xx_cs {
138 u8 mode;
139};
140
141static int spi_ppc4xx_txrx(struct spi_device *spi, struct spi_transfer *t)
142{
143 struct ppc4xx_spi *hw;
144 u8 data;
145
146 dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
147 t->tx_buf, t->rx_buf, t->len);
148
149 hw = spi_master_get_devdata(spi->master);
150
151 hw->tx = t->tx_buf;
152 hw->rx = t->rx_buf;
153 hw->len = t->len;
154 hw->count = 0;
155
156 /* send the first byte */
157 data = hw->tx ? hw->tx[0] : 0;
158 out_8(&hw->regs->txd, data);
159 out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR);
160 wait_for_completion(&hw->done);
161
162 return hw->count;
163}
164
165static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t)
166{
167 struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
168 struct spi_ppc4xx_cs *cs = spi->controller_state;
169 int scr;
170 u8 cdm = 0;
171 u32 speed;
172
173 /* Start with the generic configuration for this device. */
174 speed = spi->max_speed_hz;
175
176 /*
177 * Modify the configuration if the transfer overrides it. Do not allow
178 * the transfer to overwrite the generic configuration with zeros.
179 */
180 if (t) {
181 if (t->speed_hz)
182 speed = min(t->speed_hz, spi->max_speed_hz);
183 }
184
185 if (!speed || (speed > spi->max_speed_hz)) {
186 dev_err(&spi->dev, "invalid speed_hz (%d)\n", speed);
187 return -EINVAL;
188 }
189
190 /* Write new configuration */
191 out_8(&hw->regs->mode, cs->mode);
192
193 /* Set the clock */
194 /* opb_freq was already divided by 4 */
195 scr = (hw->opb_freq / speed) - 1;
196 if (scr > 0)
197 cdm = min(scr, 0xff);
198
199 dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm, speed);
200
201 if (in_8(&hw->regs->cdm) != cdm)
202 out_8(&hw->regs->cdm, cdm);
203
204 mutex_lock(&hw->bitbang.lock);
205 if (!hw->bitbang.busy) {
206 hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
207 /* Need to ndelay here? */
208 }
209 mutex_unlock(&hw->bitbang.lock);
210
211 return 0;
212}
213
214static int spi_ppc4xx_setup(struct spi_device *spi)
215{
216 struct spi_ppc4xx_cs *cs = spi->controller_state;
217
218 if (!spi->max_speed_hz) {
219 dev_err(&spi->dev, "invalid max_speed_hz (must be non-zero)\n");
220 return -EINVAL;
221 }
222
223 if (cs == NULL) {
224 cs = kzalloc(sizeof *cs, GFP_KERNEL);
225 if (!cs)
226 return -ENOMEM;
227 spi->controller_state = cs;
228 }
229
230 /*
231 * We set all bits of the SPI0_MODE register, so,
232 * no need to read-modify-write
233 */
234 cs->mode = SPI_PPC4XX_MODE_SPE;
235
236 switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
237 case SPI_MODE_0:
238 cs->mode |= SPI_CLK_MODE0;
239 break;
240 case SPI_MODE_1:
241 cs->mode |= SPI_CLK_MODE1;
242 break;
243 case SPI_MODE_2:
244 cs->mode |= SPI_CLK_MODE2;
245 break;
246 case SPI_MODE_3:
247 cs->mode |= SPI_CLK_MODE3;
248 break;
249 }
250
251 if (spi->mode & SPI_LSB_FIRST)
252 cs->mode |= SPI_PPC4XX_MODE_RD;
253
254 return 0;
255}
256
257static void spi_ppc4xx_chipsel(struct spi_device *spi, int value)
258{
259 struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
260 unsigned int cs = spi->chip_select;
261 unsigned int cspol;
262
263 /*
264 * If there are no chip selects at all, or if this is the special
265 * case of a non-existent (dummy) chip select, do nothing.
266 */
267
268 if (!hw->master->num_chipselect || hw->gpios[cs] == -EEXIST)
269 return;
270
271 cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
272 if (value == BITBANG_CS_INACTIVE)
273 cspol = !cspol;
274
275 gpio_set_value(hw->gpios[cs], cspol);
276}
277
278static irqreturn_t spi_ppc4xx_int(int irq, void *dev_id)
279{
280 struct ppc4xx_spi *hw;
281 u8 status;
282 u8 data;
283 unsigned int count;
284
285 hw = (struct ppc4xx_spi *)dev_id;
286
287 status = in_8(&hw->regs->sr);
288 if (!status)
289 return IRQ_NONE;
290
291 /*
292 * BSY de-asserts one cycle after the transfer is complete. The
293 * interrupt is asserted after the transfer is complete. The exact
294 * relationship is not documented, hence this code.
295 */
296
297 if (unlikely(status & SPI_PPC4XX_SR_BSY)) {
298 u8 lstatus;
299 int cnt = 0;
300
301 dev_dbg(hw->dev, "got interrupt but spi still busy?\n");
302 do {
303 ndelay(10);
304 lstatus = in_8(&hw->regs->sr);
305 } while (++cnt < 100 && lstatus & SPI_PPC4XX_SR_BSY);
306
307 if (cnt >= 100) {
308 dev_err(hw->dev, "busywait: too many loops!\n");
309 complete(&hw->done);
310 return IRQ_HANDLED;
311 } else {
312 /* status is always 1 (RBR) here */
313 status = in_8(&hw->regs->sr);
314 dev_dbg(hw->dev, "loops %d status %x\n", cnt, status);
315 }
316 }
317
318 count = hw->count;
319 hw->count++;
320
321 /* RBR triggered this interrupt. Therefore, data must be ready. */
322 data = in_8(&hw->regs->rxd);
323 if (hw->rx)
324 hw->rx[count] = data;
325
326 count++;
327
328 if (count < hw->len) {
329 data = hw->tx ? hw->tx[count] : 0;
330 out_8(&hw->regs->txd, data);
331 out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR);
332 } else {
333 complete(&hw->done);
334 }
335
336 return IRQ_HANDLED;
337}
338
339static void spi_ppc4xx_cleanup(struct spi_device *spi)
340{
341 kfree(spi->controller_state);
342}
343
344static void spi_ppc4xx_enable(struct ppc4xx_spi *hw)
345{
346 /*
347 * On all 4xx PPC's the SPI bus is shared/multiplexed with
348 * the 2nd I2C bus. We need to enable the the SPI bus before
349 * using it.
350 */
351
352 /* need to clear bit 14 to enable SPC */
353 dcri_clrset(SDR0, SDR0_PFC1, 0x80000000 >> 14, 0);
354}
355
356static void free_gpios(struct ppc4xx_spi *hw)
357{
358 if (hw->master->num_chipselect) {
359 int i;
360 for (i = 0; i < hw->master->num_chipselect; i++)
361 if (gpio_is_valid(hw->gpios[i]))
362 gpio_free(hw->gpios[i]);
363
364 kfree(hw->gpios);
365 hw->gpios = NULL;
366 }
367}
368
369/*
370 * platform_device layer stuff...
371 */
372static int spi_ppc4xx_of_probe(struct platform_device *op)
373{
374 struct ppc4xx_spi *hw;
375 struct spi_master *master;
376 struct spi_bitbang *bbp;
377 struct resource resource;
378 struct device_node *np = op->dev.of_node;
379 struct device *dev = &op->dev;
380 struct device_node *opbnp;
381 int ret;
382 int num_gpios;
383 const unsigned int *clk;
384
385 master = spi_alloc_master(dev, sizeof *hw);
386 if (master == NULL)
387 return -ENOMEM;
388 master->dev.of_node = np;
389 platform_set_drvdata(op, master);
390 hw = spi_master_get_devdata(master);
391 hw->master = master;
392 hw->dev = dev;
393
394 init_completion(&hw->done);
395
396 /*
397 * A count of zero implies a single SPI device without any chip-select.
398 * Note that of_gpio_count counts all gpios assigned to this spi master.
399 * This includes both "null" gpio's and real ones.
400 */
401 num_gpios = of_gpio_count(np);
402 if (num_gpios > 0) {
403 int i;
404
405 hw->gpios = kcalloc(num_gpios, sizeof(*hw->gpios), GFP_KERNEL);
406 if (!hw->gpios) {
407 ret = -ENOMEM;
408 goto free_master;
409 }
410
411 for (i = 0; i < num_gpios; i++) {
412 int gpio;
413 enum of_gpio_flags flags;
414
415 gpio = of_get_gpio_flags(np, i, &flags);
416 hw->gpios[i] = gpio;
417
418 if (gpio_is_valid(gpio)) {
419 /* Real CS - set the initial state. */
420 ret = gpio_request(gpio, np->name);
421 if (ret < 0) {
422 dev_err(dev,
423 "can't request gpio #%d: %d\n",
424 i, ret);
425 goto free_gpios;
426 }
427
428 gpio_direction_output(gpio,
429 !!(flags & OF_GPIO_ACTIVE_LOW));
430 } else if (gpio == -EEXIST) {
431 ; /* No CS, but that's OK. */
432 } else {
433 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
434 ret = -EINVAL;
435 goto free_gpios;
436 }
437 }
438 }
439
440 /* Setup the state for the bitbang driver */
441 bbp = &hw->bitbang;
442 bbp->master = hw->master;
443 bbp->setup_transfer = spi_ppc4xx_setupxfer;
444 bbp->chipselect = spi_ppc4xx_chipsel;
445 bbp->txrx_bufs = spi_ppc4xx_txrx;
446 bbp->use_dma = 0;
447 bbp->master->setup = spi_ppc4xx_setup;
448 bbp->master->cleanup = spi_ppc4xx_cleanup;
449 bbp->master->bits_per_word_mask = SPI_BPW_MASK(8);
450
451 /* the spi->mode bits understood by this driver: */
452 bbp->master->mode_bits =
453 SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST;
454
455 /* this many pins in all GPIO controllers */
456 bbp->master->num_chipselect = num_gpios > 0 ? num_gpios : 0;
457
458 /* Get the clock for the OPB */
459 opbnp = of_find_compatible_node(NULL, NULL, "ibm,opb");
460 if (opbnp == NULL) {
461 dev_err(dev, "OPB: cannot find node\n");
462 ret = -ENODEV;
463 goto free_gpios;
464 }
465 /* Get the clock (Hz) for the OPB */
466 clk = of_get_property(opbnp, "clock-frequency", NULL);
467 if (clk == NULL) {
468 dev_err(dev, "OPB: no clock-frequency property set\n");
469 of_node_put(opbnp);
470 ret = -ENODEV;
471 goto free_gpios;
472 }
473 hw->opb_freq = *clk;
474 hw->opb_freq >>= 2;
475 of_node_put(opbnp);
476
477 ret = of_address_to_resource(np, 0, &resource);
478 if (ret) {
479 dev_err(dev, "error while parsing device node resource\n");
480 goto free_gpios;
481 }
482 hw->mapbase = resource.start;
483 hw->mapsize = resource_size(&resource);
484
485 /* Sanity check */
486 if (hw->mapsize < sizeof(struct spi_ppc4xx_regs)) {
487 dev_err(dev, "too small to map registers\n");
488 ret = -EINVAL;
489 goto free_gpios;
490 }
491
492 /* Request IRQ */
493 ret = platform_get_irq(op, 0);
494 if (ret < 0)
495 goto free_host;
496 hw->irqnum = ret;
497
498 ret = request_irq(hw->irqnum, spi_ppc4xx_int,
499 0, "spi_ppc4xx_of", (void *)hw);
500 if (ret) {
501 dev_err(dev, "unable to allocate interrupt\n");
502 goto free_gpios;
503 }
504
505 if (!request_mem_region(hw->mapbase, hw->mapsize, DRIVER_NAME)) {
506 dev_err(dev, "resource unavailable\n");
507 ret = -EBUSY;
508 goto request_mem_error;
509 }
510
511 hw->regs = ioremap(hw->mapbase, sizeof(struct spi_ppc4xx_regs));
512
513 if (!hw->regs) {
514 dev_err(dev, "unable to memory map registers\n");
515 ret = -ENXIO;
516 goto map_io_error;
517 }
518
519 spi_ppc4xx_enable(hw);
520
521 /* Finally register our spi controller */
522 dev->dma_mask = 0;
523 ret = spi_bitbang_start(bbp);
524 if (ret) {
525 dev_err(dev, "failed to register SPI master\n");
526 goto unmap_regs;
527 }
528
529 dev_info(dev, "driver initialized\n");
530
531 return 0;
532
533unmap_regs:
534 iounmap(hw->regs);
535map_io_error:
536 release_mem_region(hw->mapbase, hw->mapsize);
537request_mem_error:
538 free_irq(hw->irqnum, hw);
539free_gpios:
540 free_gpios(hw);
541free_master:
542 spi_master_put(master);
543
544 dev_err(dev, "initialization failed\n");
545 return ret;
546}
547
548static int spi_ppc4xx_of_remove(struct platform_device *op)
549{
550 struct spi_master *master = platform_get_drvdata(op);
551 struct ppc4xx_spi *hw = spi_master_get_devdata(master);
552
553 spi_bitbang_stop(&hw->bitbang);
554 release_mem_region(hw->mapbase, hw->mapsize);
555 free_irq(hw->irqnum, hw);
556 iounmap(hw->regs);
557 free_gpios(hw);
558 spi_master_put(master);
559 return 0;
560}
561
562static const struct of_device_id spi_ppc4xx_of_match[] = {
563 { .compatible = "ibm,ppc4xx-spi", },
564 {},
565};
566
567MODULE_DEVICE_TABLE(of, spi_ppc4xx_of_match);
568
569static struct platform_driver spi_ppc4xx_of_driver = {
570 .probe = spi_ppc4xx_of_probe,
571 .remove = spi_ppc4xx_of_remove,
572 .driver = {
573 .name = DRIVER_NAME,
574 .of_match_table = spi_ppc4xx_of_match,
575 },
576};
577module_platform_driver(spi_ppc4xx_of_driver);
578
579MODULE_AUTHOR("Gary Jennejohn & Stefan Roese");
580MODULE_DESCRIPTION("Simple PPC4xx SPI Driver");
581MODULE_LICENSE("GPL");