| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Freescale SPI controller driver. |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 13 | 14 | * GRLIB support: |
|---|
| 14 | 15 | * Copyright (c) 2012 Aeroflex Gaisler AB. |
|---|
| 15 | 16 | * Author: Andreas Larsson <andreas@gaisler.com> |
|---|
| 16 | | - * |
|---|
| 17 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 18 | | - * under the terms of the GNU General Public License as published by the |
|---|
| 19 | | - * Free Software Foundation; either version 2 of the License, or (at your |
|---|
| 20 | | - * option) any later version. |
|---|
| 21 | 17 | */ |
|---|
| 22 | 18 | #include <linux/delay.h> |
|---|
| 23 | 19 | #include <linux/dma-mapping.h> |
|---|
| 24 | 20 | #include <linux/fsl_devices.h> |
|---|
| 25 | | -#include <linux/gpio.h> |
|---|
| 21 | +#include <linux/gpio/consumer.h> |
|---|
| 26 | 22 | #include <linux/interrupt.h> |
|---|
| 27 | 23 | #include <linux/irq.h> |
|---|
| 28 | 24 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 32 | 28 | #include <linux/of.h> |
|---|
| 33 | 29 | #include <linux/of_address.h> |
|---|
| 34 | 30 | #include <linux/of_irq.h> |
|---|
| 35 | | -#include <linux/of_gpio.h> |
|---|
| 36 | 31 | #include <linux/of_platform.h> |
|---|
| 37 | 32 | #include <linux/platform_device.h> |
|---|
| 38 | 33 | #include <linux/spi/spi.h> |
|---|
| 39 | 34 | #include <linux/spi/spi_bitbang.h> |
|---|
| 40 | 35 | #include <linux/types.h> |
|---|
| 36 | + |
|---|
| 37 | +#ifdef CONFIG_FSL_SOC |
|---|
| 38 | +#include <sysdev/fsl_soc.h> |
|---|
| 39 | +#endif |
|---|
| 40 | + |
|---|
| 41 | +/* Specific to the MPC8306/MPC8309 */ |
|---|
| 42 | +#define IMMR_SPI_CS_OFFSET 0x14c |
|---|
| 43 | +#define SPI_BOOT_SEL_BIT 0x80000000 |
|---|
| 41 | 44 | |
|---|
| 42 | 45 | #include "spi-fsl-lib.h" |
|---|
| 43 | 46 | #include "spi-fsl-cpm.h" |
|---|
| .. | .. |
|---|
| 87 | 90 | { |
|---|
| 88 | 91 | struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master); |
|---|
| 89 | 92 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
|---|
| 90 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
|---|
| 93 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
|---|
| 91 | 94 | __be32 __iomem *mode = ®_base->mode; |
|---|
| 92 | 95 | unsigned long flags; |
|---|
| 93 | 96 | |
|---|
| .. | .. |
|---|
| 112 | 115 | { |
|---|
| 113 | 116 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
|---|
| 114 | 117 | struct fsl_spi_platform_data *pdata; |
|---|
| 115 | | - bool pol = spi->mode & SPI_CS_HIGH; |
|---|
| 116 | 118 | struct spi_mpc8xxx_cs *cs = spi->controller_state; |
|---|
| 117 | 119 | |
|---|
| 118 | 120 | pdata = spi->dev.parent->parent->platform_data; |
|---|
| 119 | 121 | |
|---|
| 120 | 122 | if (value == BITBANG_CS_INACTIVE) { |
|---|
| 121 | 123 | if (pdata->cs_control) |
|---|
| 122 | | - pdata->cs_control(spi, !pol); |
|---|
| 124 | + pdata->cs_control(spi, false); |
|---|
| 123 | 125 | } |
|---|
| 124 | 126 | |
|---|
| 125 | 127 | if (value == BITBANG_CS_ACTIVE) { |
|---|
| .. | .. |
|---|
| 131 | 133 | fsl_spi_change_mode(spi); |
|---|
| 132 | 134 | |
|---|
| 133 | 135 | if (pdata->cs_control) |
|---|
| 134 | | - pdata->cs_control(spi, pol); |
|---|
| 136 | + pdata->cs_control(spi, true); |
|---|
| 135 | 137 | } |
|---|
| 136 | 138 | } |
|---|
| 137 | 139 | |
|---|
| .. | .. |
|---|
| 288 | 290 | struct spi_transfer *t, unsigned int len) |
|---|
| 289 | 291 | { |
|---|
| 290 | 292 | u32 word; |
|---|
| 291 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
|---|
| 293 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
|---|
| 292 | 294 | |
|---|
| 293 | 295 | mspi->count = len; |
|---|
| 294 | 296 | |
|---|
| .. | .. |
|---|
| 306 | 308 | bool is_dma_mapped) |
|---|
| 307 | 309 | { |
|---|
| 308 | 310 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
|---|
| 309 | | - struct fsl_spi_reg *reg_base; |
|---|
| 311 | + struct fsl_spi_reg __iomem *reg_base; |
|---|
| 310 | 312 | unsigned int len = t->len; |
|---|
| 311 | 313 | u8 bits_per_word; |
|---|
| 312 | 314 | int ret; |
|---|
| .. | .. |
|---|
| 355 | 357 | static int fsl_spi_do_one_msg(struct spi_master *master, |
|---|
| 356 | 358 | struct spi_message *m) |
|---|
| 357 | 359 | { |
|---|
| 360 | + struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
|---|
| 358 | 361 | struct spi_device *spi = m->spi; |
|---|
| 359 | 362 | struct spi_transfer *t, *first; |
|---|
| 360 | 363 | unsigned int cs_change; |
|---|
| 361 | 364 | const int nsecs = 50; |
|---|
| 362 | | - int status; |
|---|
| 365 | + int status, last_bpw; |
|---|
| 366 | + |
|---|
| 367 | + /* |
|---|
| 368 | + * In CPU mode, optimize large byte transfers to use larger |
|---|
| 369 | + * bits_per_word values to reduce number of interrupts taken. |
|---|
| 370 | + */ |
|---|
| 371 | + if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) { |
|---|
| 372 | + list_for_each_entry(t, &m->transfers, transfer_list) { |
|---|
| 373 | + if (t->len < 256 || t->bits_per_word != 8) |
|---|
| 374 | + continue; |
|---|
| 375 | + if ((t->len & 3) == 0) |
|---|
| 376 | + t->bits_per_word = 32; |
|---|
| 377 | + else if ((t->len & 1) == 0) |
|---|
| 378 | + t->bits_per_word = 16; |
|---|
| 379 | + } |
|---|
| 380 | + } |
|---|
| 363 | 381 | |
|---|
| 364 | 382 | /* Don't allow changes if CS is active */ |
|---|
| 365 | | - first = list_first_entry(&m->transfers, struct spi_transfer, |
|---|
| 366 | | - transfer_list); |
|---|
| 383 | + cs_change = 1; |
|---|
| 367 | 384 | list_for_each_entry(t, &m->transfers, transfer_list) { |
|---|
| 368 | | - if ((first->bits_per_word != t->bits_per_word) || |
|---|
| 369 | | - (first->speed_hz != t->speed_hz)) { |
|---|
| 385 | + if (cs_change) |
|---|
| 386 | + first = t; |
|---|
| 387 | + cs_change = t->cs_change; |
|---|
| 388 | + if (first->speed_hz != t->speed_hz) { |
|---|
| 370 | 389 | dev_err(&spi->dev, |
|---|
| 371 | | - "bits_per_word/speed_hz should be same for the same SPI transfer\n"); |
|---|
| 390 | + "speed_hz cannot change while CS is active\n"); |
|---|
| 372 | 391 | return -EINVAL; |
|---|
| 373 | 392 | } |
|---|
| 374 | 393 | } |
|---|
| 375 | 394 | |
|---|
| 395 | + last_bpw = -1; |
|---|
| 376 | 396 | cs_change = 1; |
|---|
| 377 | 397 | status = -EINVAL; |
|---|
| 378 | 398 | list_for_each_entry(t, &m->transfers, transfer_list) { |
|---|
| 379 | | - if (t->bits_per_word || t->speed_hz) { |
|---|
| 380 | | - if (cs_change) |
|---|
| 381 | | - status = fsl_spi_setup_transfer(spi, t); |
|---|
| 382 | | - if (status < 0) |
|---|
| 383 | | - break; |
|---|
| 384 | | - } |
|---|
| 399 | + if (cs_change || last_bpw != t->bits_per_word) |
|---|
| 400 | + status = fsl_spi_setup_transfer(spi, t); |
|---|
| 401 | + if (status < 0) |
|---|
| 402 | + break; |
|---|
| 403 | + last_bpw = t->bits_per_word; |
|---|
| 385 | 404 | |
|---|
| 386 | 405 | if (cs_change) { |
|---|
| 387 | 406 | fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE); |
|---|
| .. | .. |
|---|
| 396 | 415 | } |
|---|
| 397 | 416 | m->actual_length += t->len; |
|---|
| 398 | 417 | |
|---|
| 399 | | - if (t->delay_usecs) |
|---|
| 400 | | - udelay(t->delay_usecs); |
|---|
| 418 | + spi_transfer_delay_exec(t); |
|---|
| 401 | 419 | |
|---|
| 402 | 420 | if (cs_change) { |
|---|
| 403 | 421 | ndelay(nsecs); |
|---|
| .. | .. |
|---|
| 421 | 439 | static int fsl_spi_setup(struct spi_device *spi) |
|---|
| 422 | 440 | { |
|---|
| 423 | 441 | struct mpc8xxx_spi *mpc8xxx_spi; |
|---|
| 424 | | - struct fsl_spi_reg *reg_base; |
|---|
| 442 | + struct fsl_spi_reg __iomem *reg_base; |
|---|
| 443 | + bool initial_setup = false; |
|---|
| 425 | 444 | int retval; |
|---|
| 426 | 445 | u32 hw_mode; |
|---|
| 427 | 446 | struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); |
|---|
| .. | .. |
|---|
| 434 | 453 | if (!cs) |
|---|
| 435 | 454 | return -ENOMEM; |
|---|
| 436 | 455 | spi_set_ctldata(spi, cs); |
|---|
| 456 | + initial_setup = true; |
|---|
| 437 | 457 | } |
|---|
| 438 | 458 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
|---|
| 439 | 459 | |
|---|
| .. | .. |
|---|
| 457 | 477 | retval = fsl_spi_setup_transfer(spi, NULL); |
|---|
| 458 | 478 | if (retval < 0) { |
|---|
| 459 | 479 | cs->hw_mode = hw_mode; /* Restore settings */ |
|---|
| 480 | + if (initial_setup) |
|---|
| 481 | + kfree(cs); |
|---|
| 460 | 482 | return retval; |
|---|
| 461 | | - } |
|---|
| 462 | | - |
|---|
| 463 | | - if (mpc8xxx_spi->type == TYPE_GRLIB) { |
|---|
| 464 | | - if (gpio_is_valid(spi->cs_gpio)) { |
|---|
| 465 | | - int desel; |
|---|
| 466 | | - |
|---|
| 467 | | - retval = gpio_request(spi->cs_gpio, |
|---|
| 468 | | - dev_name(&spi->dev)); |
|---|
| 469 | | - if (retval) |
|---|
| 470 | | - return retval; |
|---|
| 471 | | - |
|---|
| 472 | | - desel = !(spi->mode & SPI_CS_HIGH); |
|---|
| 473 | | - retval = gpio_direction_output(spi->cs_gpio, desel); |
|---|
| 474 | | - if (retval) { |
|---|
| 475 | | - gpio_free(spi->cs_gpio); |
|---|
| 476 | | - return retval; |
|---|
| 477 | | - } |
|---|
| 478 | | - } else if (spi->cs_gpio != -ENOENT) { |
|---|
| 479 | | - if (spi->cs_gpio < 0) |
|---|
| 480 | | - return spi->cs_gpio; |
|---|
| 481 | | - return -EINVAL; |
|---|
| 482 | | - } |
|---|
| 483 | | - /* When spi->cs_gpio == -ENOENT, a hole in the phandle list |
|---|
| 484 | | - * indicates to use native chipselect if present, or allow for |
|---|
| 485 | | - * an always selected chip |
|---|
| 486 | | - */ |
|---|
| 487 | 483 | } |
|---|
| 488 | 484 | |
|---|
| 489 | 485 | /* Initialize chipselect - might be active for SPI_CS_HIGH mode */ |
|---|
| .. | .. |
|---|
| 494 | 490 | |
|---|
| 495 | 491 | static void fsl_spi_cleanup(struct spi_device *spi) |
|---|
| 496 | 492 | { |
|---|
| 497 | | - struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
|---|
| 498 | 493 | struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); |
|---|
| 499 | | - |
|---|
| 500 | | - if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio)) |
|---|
| 501 | | - gpio_free(spi->cs_gpio); |
|---|
| 502 | 494 | |
|---|
| 503 | 495 | kfree(cs); |
|---|
| 504 | 496 | spi_set_ctldata(spi, NULL); |
|---|
| .. | .. |
|---|
| 506 | 498 | |
|---|
| 507 | 499 | static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
|---|
| 508 | 500 | { |
|---|
| 509 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
|---|
| 501 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
|---|
| 510 | 502 | |
|---|
| 511 | 503 | /* We need handle RX first */ |
|---|
| 512 | 504 | if (events & SPIE_NE) { |
|---|
| .. | .. |
|---|
| 541 | 533 | struct mpc8xxx_spi *mspi = context_data; |
|---|
| 542 | 534 | irqreturn_t ret = IRQ_NONE; |
|---|
| 543 | 535 | u32 events; |
|---|
| 544 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
|---|
| 536 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
|---|
| 545 | 537 | |
|---|
| 546 | 538 | /* Get interrupt events(tx/rx) */ |
|---|
| 547 | 539 | events = mpc8xxx_spi_read_reg(®_base->event); |
|---|
| .. | .. |
|---|
| 561 | 553 | static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on) |
|---|
| 562 | 554 | { |
|---|
| 563 | 555 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
|---|
| 564 | | - struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base; |
|---|
| 556 | + struct fsl_spi_reg __iomem *reg_base = mpc8xxx_spi->reg_base; |
|---|
| 565 | 557 | u32 slvsel; |
|---|
| 566 | 558 | u16 cs = spi->chip_select; |
|---|
| 567 | 559 | |
|---|
| 568 | | - if (gpio_is_valid(spi->cs_gpio)) { |
|---|
| 569 | | - gpio_set_value(spi->cs_gpio, on); |
|---|
| 560 | + if (spi->cs_gpiod) { |
|---|
| 561 | + gpiod_set_value(spi->cs_gpiod, on); |
|---|
| 570 | 562 | } else if (cs < mpc8xxx_spi->native_chipselects) { |
|---|
| 571 | 563 | slvsel = mpc8xxx_spi_read_reg(®_base->slvsel); |
|---|
| 572 | 564 | slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs)); |
|---|
| .. | .. |
|---|
| 579 | 571 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 580 | 572 | struct spi_master *master = dev_get_drvdata(dev); |
|---|
| 581 | 573 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
|---|
| 582 | | - struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base; |
|---|
| 574 | + struct fsl_spi_reg __iomem *reg_base = mpc8xxx_spi->reg_base; |
|---|
| 583 | 575 | int mbits; |
|---|
| 584 | 576 | u32 capabilities; |
|---|
| 585 | 577 | |
|---|
| .. | .. |
|---|
| 599 | 591 | pdata->cs_control = fsl_spi_grlib_cs_control; |
|---|
| 600 | 592 | } |
|---|
| 601 | 593 | |
|---|
| 602 | | -static struct spi_master * fsl_spi_probe(struct device *dev, |
|---|
| 594 | +static struct spi_master *fsl_spi_probe(struct device *dev, |
|---|
| 603 | 595 | struct resource *mem, unsigned int irq) |
|---|
| 604 | 596 | { |
|---|
| 605 | 597 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 606 | 598 | struct spi_master *master; |
|---|
| 607 | 599 | struct mpc8xxx_spi *mpc8xxx_spi; |
|---|
| 608 | | - struct fsl_spi_reg *reg_base; |
|---|
| 600 | + struct fsl_spi_reg __iomem *reg_base; |
|---|
| 609 | 601 | u32 regval; |
|---|
| 610 | 602 | int ret = 0; |
|---|
| 611 | 603 | |
|---|
| .. | .. |
|---|
| 622 | 614 | master->setup = fsl_spi_setup; |
|---|
| 623 | 615 | master->cleanup = fsl_spi_cleanup; |
|---|
| 624 | 616 | master->transfer_one_message = fsl_spi_do_one_msg; |
|---|
| 617 | + master->use_gpio_descriptors = true; |
|---|
| 625 | 618 | |
|---|
| 626 | 619 | mpc8xxx_spi = spi_master_get_devdata(master); |
|---|
| 627 | 620 | mpc8xxx_spi->max_bits_per_word = 32; |
|---|
| .. | .. |
|---|
| 697 | 690 | |
|---|
| 698 | 691 | static void fsl_spi_cs_control(struct spi_device *spi, bool on) |
|---|
| 699 | 692 | { |
|---|
| 700 | | - struct device *dev = spi->dev.parent->parent; |
|---|
| 701 | | - struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 702 | | - struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
|---|
| 703 | | - u16 cs = spi->chip_select; |
|---|
| 704 | | - int gpio = pinfo->gpios[cs]; |
|---|
| 705 | | - bool alow = pinfo->alow_flags[cs]; |
|---|
| 693 | + if (spi->cs_gpiod) { |
|---|
| 694 | + gpiod_set_value(spi->cs_gpiod, on); |
|---|
| 695 | + } else { |
|---|
| 696 | + struct device *dev = spi->dev.parent->parent; |
|---|
| 697 | + struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 698 | + struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
|---|
| 706 | 699 | |
|---|
| 707 | | - gpio_set_value(gpio, on ^ alow); |
|---|
| 708 | | -} |
|---|
| 709 | | - |
|---|
| 710 | | -static int of_fsl_spi_get_chipselects(struct device *dev) |
|---|
| 711 | | -{ |
|---|
| 712 | | - struct device_node *np = dev->of_node; |
|---|
| 713 | | - struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 714 | | - struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
|---|
| 715 | | - int ngpios; |
|---|
| 716 | | - int i = 0; |
|---|
| 717 | | - int ret; |
|---|
| 718 | | - |
|---|
| 719 | | - ngpios = of_gpio_count(np); |
|---|
| 720 | | - if (ngpios <= 0) { |
|---|
| 721 | | - /* |
|---|
| 722 | | - * SPI w/o chip-select line. One SPI device is still permitted |
|---|
| 723 | | - * though. |
|---|
| 724 | | - */ |
|---|
| 725 | | - pdata->max_chipselect = 1; |
|---|
| 726 | | - return 0; |
|---|
| 700 | + if (WARN_ON_ONCE(!pinfo->immr_spi_cs)) |
|---|
| 701 | + return; |
|---|
| 702 | + iowrite32be(on ? 0 : SPI_BOOT_SEL_BIT, pinfo->immr_spi_cs); |
|---|
| 727 | 703 | } |
|---|
| 728 | | - |
|---|
| 729 | | - pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios), |
|---|
| 730 | | - GFP_KERNEL); |
|---|
| 731 | | - if (!pinfo->gpios) |
|---|
| 732 | | - return -ENOMEM; |
|---|
| 733 | | - memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios)); |
|---|
| 734 | | - |
|---|
| 735 | | - pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags), |
|---|
| 736 | | - GFP_KERNEL); |
|---|
| 737 | | - if (!pinfo->alow_flags) { |
|---|
| 738 | | - ret = -ENOMEM; |
|---|
| 739 | | - goto err_alloc_flags; |
|---|
| 740 | | - } |
|---|
| 741 | | - |
|---|
| 742 | | - for (; i < ngpios; i++) { |
|---|
| 743 | | - int gpio; |
|---|
| 744 | | - enum of_gpio_flags flags; |
|---|
| 745 | | - |
|---|
| 746 | | - gpio = of_get_gpio_flags(np, i, &flags); |
|---|
| 747 | | - if (!gpio_is_valid(gpio)) { |
|---|
| 748 | | - dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); |
|---|
| 749 | | - ret = gpio; |
|---|
| 750 | | - goto err_loop; |
|---|
| 751 | | - } |
|---|
| 752 | | - |
|---|
| 753 | | - ret = gpio_request(gpio, dev_name(dev)); |
|---|
| 754 | | - if (ret) { |
|---|
| 755 | | - dev_err(dev, "can't request gpio #%d: %d\n", i, ret); |
|---|
| 756 | | - goto err_loop; |
|---|
| 757 | | - } |
|---|
| 758 | | - |
|---|
| 759 | | - pinfo->gpios[i] = gpio; |
|---|
| 760 | | - pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW; |
|---|
| 761 | | - |
|---|
| 762 | | - ret = gpio_direction_output(pinfo->gpios[i], |
|---|
| 763 | | - pinfo->alow_flags[i]); |
|---|
| 764 | | - if (ret) { |
|---|
| 765 | | - dev_err(dev, |
|---|
| 766 | | - "can't set output direction for gpio #%d: %d\n", |
|---|
| 767 | | - i, ret); |
|---|
| 768 | | - goto err_loop; |
|---|
| 769 | | - } |
|---|
| 770 | | - } |
|---|
| 771 | | - |
|---|
| 772 | | - pdata->max_chipselect = ngpios; |
|---|
| 773 | | - pdata->cs_control = fsl_spi_cs_control; |
|---|
| 774 | | - |
|---|
| 775 | | - return 0; |
|---|
| 776 | | - |
|---|
| 777 | | -err_loop: |
|---|
| 778 | | - while (i >= 0) { |
|---|
| 779 | | - if (gpio_is_valid(pinfo->gpios[i])) |
|---|
| 780 | | - gpio_free(pinfo->gpios[i]); |
|---|
| 781 | | - i--; |
|---|
| 782 | | - } |
|---|
| 783 | | - |
|---|
| 784 | | - kfree(pinfo->alow_flags); |
|---|
| 785 | | - pinfo->alow_flags = NULL; |
|---|
| 786 | | -err_alloc_flags: |
|---|
| 787 | | - kfree(pinfo->gpios); |
|---|
| 788 | | - pinfo->gpios = NULL; |
|---|
| 789 | | - return ret; |
|---|
| 790 | | -} |
|---|
| 791 | | - |
|---|
| 792 | | -static int of_fsl_spi_free_chipselects(struct device *dev) |
|---|
| 793 | | -{ |
|---|
| 794 | | - struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 795 | | - struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
|---|
| 796 | | - int i; |
|---|
| 797 | | - |
|---|
| 798 | | - if (!pinfo->gpios) |
|---|
| 799 | | - return 0; |
|---|
| 800 | | - |
|---|
| 801 | | - for (i = 0; i < pdata->max_chipselect; i++) { |
|---|
| 802 | | - if (gpio_is_valid(pinfo->gpios[i])) |
|---|
| 803 | | - gpio_free(pinfo->gpios[i]); |
|---|
| 804 | | - } |
|---|
| 805 | | - |
|---|
| 806 | | - kfree(pinfo->gpios); |
|---|
| 807 | | - kfree(pinfo->alow_flags); |
|---|
| 808 | | - return 0; |
|---|
| 809 | 704 | } |
|---|
| 810 | 705 | |
|---|
| 811 | 706 | static int of_fsl_spi_probe(struct platform_device *ofdev) |
|---|
| .. | .. |
|---|
| 814 | 709 | struct device_node *np = ofdev->dev.of_node; |
|---|
| 815 | 710 | struct spi_master *master; |
|---|
| 816 | 711 | struct resource mem; |
|---|
| 817 | | - int irq = 0, type; |
|---|
| 818 | | - int ret = -ENOMEM; |
|---|
| 712 | + int irq, type; |
|---|
| 713 | + int ret; |
|---|
| 714 | + bool spisel_boot = false; |
|---|
| 715 | +#if IS_ENABLED(CONFIG_FSL_SOC) |
|---|
| 716 | + struct mpc8xxx_spi_probe_info *pinfo = NULL; |
|---|
| 717 | +#endif |
|---|
| 718 | + |
|---|
| 819 | 719 | |
|---|
| 820 | 720 | ret = of_mpc8xxx_spi_probe(ofdev); |
|---|
| 821 | 721 | if (ret) |
|---|
| .. | .. |
|---|
| 823 | 723 | |
|---|
| 824 | 724 | type = fsl_spi_get_type(&ofdev->dev); |
|---|
| 825 | 725 | if (type == TYPE_FSL) { |
|---|
| 826 | | - ret = of_fsl_spi_get_chipselects(dev); |
|---|
| 827 | | - if (ret) |
|---|
| 828 | | - goto err; |
|---|
| 726 | + struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
|---|
| 727 | +#if IS_ENABLED(CONFIG_FSL_SOC) |
|---|
| 728 | + pinfo = to_of_pinfo(pdata); |
|---|
| 729 | + |
|---|
| 730 | + spisel_boot = of_property_read_bool(np, "fsl,spisel_boot"); |
|---|
| 731 | + if (spisel_boot) { |
|---|
| 732 | + pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4); |
|---|
| 733 | + if (!pinfo->immr_spi_cs) |
|---|
| 734 | + return -ENOMEM; |
|---|
| 735 | + } |
|---|
| 736 | +#endif |
|---|
| 737 | + /* |
|---|
| 738 | + * Handle the case where we have one hardwired (always selected) |
|---|
| 739 | + * device on the first "chipselect". Else we let the core code |
|---|
| 740 | + * handle any GPIOs or native chip selects and assign the |
|---|
| 741 | + * appropriate callback for dealing with the CS lines. This isn't |
|---|
| 742 | + * supported on the GRLIB variant. |
|---|
| 743 | + */ |
|---|
| 744 | + ret = gpiod_count(dev, "cs"); |
|---|
| 745 | + if (ret < 0) |
|---|
| 746 | + ret = 0; |
|---|
| 747 | + if (ret == 0 && !spisel_boot) { |
|---|
| 748 | + pdata->max_chipselect = 1; |
|---|
| 749 | + } else { |
|---|
| 750 | + pdata->max_chipselect = ret + spisel_boot; |
|---|
| 751 | + pdata->cs_control = fsl_spi_cs_control; |
|---|
| 752 | + } |
|---|
| 829 | 753 | } |
|---|
| 830 | 754 | |
|---|
| 831 | 755 | ret = of_address_to_resource(np, 0, &mem); |
|---|
| 832 | 756 | if (ret) |
|---|
| 833 | | - goto err; |
|---|
| 757 | + goto unmap_out; |
|---|
| 834 | 758 | |
|---|
| 835 | 759 | irq = platform_get_irq(ofdev, 0); |
|---|
| 836 | 760 | if (irq < 0) { |
|---|
| 837 | 761 | ret = irq; |
|---|
| 838 | | - goto err; |
|---|
| 762 | + goto unmap_out; |
|---|
| 839 | 763 | } |
|---|
| 840 | 764 | |
|---|
| 841 | 765 | master = fsl_spi_probe(dev, &mem, irq); |
|---|
| 842 | | - if (IS_ERR(master)) { |
|---|
| 843 | | - ret = PTR_ERR(master); |
|---|
| 844 | | - goto err; |
|---|
| 845 | | - } |
|---|
| 846 | 766 | |
|---|
| 847 | | - return 0; |
|---|
| 767 | + return PTR_ERR_OR_ZERO(master); |
|---|
| 848 | 768 | |
|---|
| 849 | | -err: |
|---|
| 850 | | - if (type == TYPE_FSL) |
|---|
| 851 | | - of_fsl_spi_free_chipselects(dev); |
|---|
| 769 | +unmap_out: |
|---|
| 770 | +#if IS_ENABLED(CONFIG_FSL_SOC) |
|---|
| 771 | + if (spisel_boot) |
|---|
| 772 | + iounmap(pinfo->immr_spi_cs); |
|---|
| 773 | +#endif |
|---|
| 852 | 774 | return ret; |
|---|
| 853 | 775 | } |
|---|
| 854 | 776 | |
|---|
| .. | .. |
|---|
| 858 | 780 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
|---|
| 859 | 781 | |
|---|
| 860 | 782 | fsl_spi_cpm_free(mpc8xxx_spi); |
|---|
| 861 | | - if (mpc8xxx_spi->type == TYPE_FSL) |
|---|
| 862 | | - of_fsl_spi_free_chipselects(&ofdev->dev); |
|---|
| 863 | 783 | return 0; |
|---|
| 864 | 784 | } |
|---|
| 865 | 785 | |
|---|