.. | .. |
---|
| 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 | |
---|
.. | .. |
---|
201 | 203 | return bits_per_word; |
---|
202 | 204 | } |
---|
203 | 205 | |
---|
204 | | -static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs, |
---|
205 | | - struct spi_device *spi, |
---|
206 | | - int bits_per_word) |
---|
207 | | -{ |
---|
208 | | - /* QE uses Little Endian for words > 8 |
---|
209 | | - * so transform all words > 8 into 8 bits |
---|
210 | | - * Unfortnatly that doesn't work for LSB so |
---|
211 | | - * reject these for now */ |
---|
212 | | - /* Note: 32 bits word, LSB works iff |
---|
213 | | - * tfcr/rfcr is set to CPMFCR_GBL */ |
---|
214 | | - if (spi->mode & SPI_LSB_FIRST && |
---|
215 | | - bits_per_word > 8) |
---|
216 | | - return -EINVAL; |
---|
217 | | - if (bits_per_word > 8) |
---|
218 | | - return 8; /* pretend its 8 bits */ |
---|
219 | | - return bits_per_word; |
---|
220 | | -} |
---|
221 | | - |
---|
222 | 206 | static int fsl_spi_setup_transfer(struct spi_device *spi, |
---|
223 | 207 | struct spi_transfer *t) |
---|
224 | 208 | { |
---|
.. | .. |
---|
246 | 230 | bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi, |
---|
247 | 231 | mpc8xxx_spi, |
---|
248 | 232 | bits_per_word); |
---|
249 | | - else if (mpc8xxx_spi->flags & SPI_QE) |
---|
250 | | - bits_per_word = mspi_apply_qe_mode_quirks(cs, spi, |
---|
251 | | - bits_per_word); |
---|
252 | 233 | |
---|
253 | 234 | if (bits_per_word < 0) |
---|
254 | 235 | return bits_per_word; |
---|
.. | .. |
---|
288 | 269 | struct spi_transfer *t, unsigned int len) |
---|
289 | 270 | { |
---|
290 | 271 | u32 word; |
---|
291 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
---|
| 272 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
---|
292 | 273 | |
---|
293 | 274 | mspi->count = len; |
---|
294 | 275 | |
---|
.. | .. |
---|
306 | 287 | bool is_dma_mapped) |
---|
307 | 288 | { |
---|
308 | 289 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
---|
309 | | - struct fsl_spi_reg *reg_base; |
---|
| 290 | + struct fsl_spi_reg __iomem *reg_base; |
---|
310 | 291 | unsigned int len = t->len; |
---|
311 | 292 | u8 bits_per_word; |
---|
312 | 293 | int ret; |
---|
.. | .. |
---|
355 | 336 | static int fsl_spi_do_one_msg(struct spi_master *master, |
---|
356 | 337 | struct spi_message *m) |
---|
357 | 338 | { |
---|
| 339 | + struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
---|
358 | 340 | struct spi_device *spi = m->spi; |
---|
359 | 341 | struct spi_transfer *t, *first; |
---|
360 | 342 | unsigned int cs_change; |
---|
361 | 343 | const int nsecs = 50; |
---|
362 | | - int status; |
---|
| 344 | + int status, last_bpw; |
---|
| 345 | + |
---|
| 346 | + /* |
---|
| 347 | + * In CPU mode, optimize large byte transfers to use larger |
---|
| 348 | + * bits_per_word values to reduce number of interrupts taken. |
---|
| 349 | + */ |
---|
| 350 | + list_for_each_entry(t, &m->transfers, transfer_list) { |
---|
| 351 | + if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) { |
---|
| 352 | + if (t->len < 256 || t->bits_per_word != 8) |
---|
| 353 | + continue; |
---|
| 354 | + if ((t->len & 3) == 0) |
---|
| 355 | + t->bits_per_word = 32; |
---|
| 356 | + else if ((t->len & 1) == 0) |
---|
| 357 | + t->bits_per_word = 16; |
---|
| 358 | + } else { |
---|
| 359 | + /* |
---|
| 360 | + * CPM/QE uses Little Endian for words > 8 |
---|
| 361 | + * so transform 16 and 32 bits words into 8 bits |
---|
| 362 | + * Unfortnatly that doesn't work for LSB so |
---|
| 363 | + * reject these for now |
---|
| 364 | + * Note: 32 bits word, LSB works iff |
---|
| 365 | + * tfcr/rfcr is set to CPMFCR_GBL |
---|
| 366 | + */ |
---|
| 367 | + if (m->spi->mode & SPI_LSB_FIRST && t->bits_per_word > 8) |
---|
| 368 | + return -EINVAL; |
---|
| 369 | + if (t->bits_per_word == 16 || t->bits_per_word == 32) |
---|
| 370 | + t->bits_per_word = 8; /* pretend its 8 bits */ |
---|
| 371 | + if (t->bits_per_word == 8 && t->len >= 256 && |
---|
| 372 | + (mpc8xxx_spi->flags & SPI_CPM1)) |
---|
| 373 | + t->bits_per_word = 16; |
---|
| 374 | + } |
---|
| 375 | + } |
---|
363 | 376 | |
---|
364 | 377 | /* Don't allow changes if CS is active */ |
---|
365 | | - first = list_first_entry(&m->transfers, struct spi_transfer, |
---|
366 | | - transfer_list); |
---|
| 378 | + cs_change = 1; |
---|
367 | 379 | 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)) { |
---|
| 380 | + if (cs_change) |
---|
| 381 | + first = t; |
---|
| 382 | + cs_change = t->cs_change; |
---|
| 383 | + if (first->speed_hz != t->speed_hz) { |
---|
370 | 384 | dev_err(&spi->dev, |
---|
371 | | - "bits_per_word/speed_hz should be same for the same SPI transfer\n"); |
---|
| 385 | + "speed_hz cannot change while CS is active\n"); |
---|
372 | 386 | return -EINVAL; |
---|
373 | 387 | } |
---|
374 | 388 | } |
---|
375 | 389 | |
---|
| 390 | + last_bpw = -1; |
---|
376 | 391 | cs_change = 1; |
---|
377 | 392 | status = -EINVAL; |
---|
378 | 393 | 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 | | - } |
---|
| 394 | + if (cs_change || last_bpw != t->bits_per_word) |
---|
| 395 | + status = fsl_spi_setup_transfer(spi, t); |
---|
| 396 | + if (status < 0) |
---|
| 397 | + break; |
---|
| 398 | + last_bpw = t->bits_per_word; |
---|
385 | 399 | |
---|
386 | 400 | if (cs_change) { |
---|
387 | 401 | fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE); |
---|
.. | .. |
---|
396 | 410 | } |
---|
397 | 411 | m->actual_length += t->len; |
---|
398 | 412 | |
---|
399 | | - if (t->delay_usecs) |
---|
400 | | - udelay(t->delay_usecs); |
---|
| 413 | + spi_transfer_delay_exec(t); |
---|
401 | 414 | |
---|
402 | 415 | if (cs_change) { |
---|
403 | 416 | ndelay(nsecs); |
---|
.. | .. |
---|
421 | 434 | static int fsl_spi_setup(struct spi_device *spi) |
---|
422 | 435 | { |
---|
423 | 436 | struct mpc8xxx_spi *mpc8xxx_spi; |
---|
424 | | - struct fsl_spi_reg *reg_base; |
---|
| 437 | + struct fsl_spi_reg __iomem *reg_base; |
---|
| 438 | + bool initial_setup = false; |
---|
425 | 439 | int retval; |
---|
426 | 440 | u32 hw_mode; |
---|
427 | 441 | struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); |
---|
.. | .. |
---|
434 | 448 | if (!cs) |
---|
435 | 449 | return -ENOMEM; |
---|
436 | 450 | spi_set_ctldata(spi, cs); |
---|
| 451 | + initial_setup = true; |
---|
437 | 452 | } |
---|
438 | 453 | mpc8xxx_spi = spi_master_get_devdata(spi->master); |
---|
439 | 454 | |
---|
.. | .. |
---|
457 | 472 | retval = fsl_spi_setup_transfer(spi, NULL); |
---|
458 | 473 | if (retval < 0) { |
---|
459 | 474 | cs->hw_mode = hw_mode; /* Restore settings */ |
---|
| 475 | + if (initial_setup) |
---|
| 476 | + kfree(cs); |
---|
460 | 477 | 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 | 478 | } |
---|
488 | 479 | |
---|
489 | 480 | /* Initialize chipselect - might be active for SPI_CS_HIGH mode */ |
---|
.. | .. |
---|
494 | 485 | |
---|
495 | 486 | static void fsl_spi_cleanup(struct spi_device *spi) |
---|
496 | 487 | { |
---|
497 | | - struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
---|
498 | 488 | 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 | 489 | |
---|
503 | 490 | kfree(cs); |
---|
504 | 491 | spi_set_ctldata(spi, NULL); |
---|
.. | .. |
---|
506 | 493 | |
---|
507 | 494 | static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) |
---|
508 | 495 | { |
---|
509 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
---|
| 496 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
---|
510 | 497 | |
---|
511 | 498 | /* We need handle RX first */ |
---|
512 | 499 | if (events & SPIE_NE) { |
---|
.. | .. |
---|
541 | 528 | struct mpc8xxx_spi *mspi = context_data; |
---|
542 | 529 | irqreturn_t ret = IRQ_NONE; |
---|
543 | 530 | u32 events; |
---|
544 | | - struct fsl_spi_reg *reg_base = mspi->reg_base; |
---|
| 531 | + struct fsl_spi_reg __iomem *reg_base = mspi->reg_base; |
---|
545 | 532 | |
---|
546 | 533 | /* Get interrupt events(tx/rx) */ |
---|
547 | 534 | events = mpc8xxx_spi_read_reg(®_base->event); |
---|
.. | .. |
---|
561 | 548 | static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on) |
---|
562 | 549 | { |
---|
563 | 550 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); |
---|
564 | | - struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base; |
---|
| 551 | + struct fsl_spi_reg __iomem *reg_base = mpc8xxx_spi->reg_base; |
---|
565 | 552 | u32 slvsel; |
---|
566 | 553 | u16 cs = spi->chip_select; |
---|
567 | 554 | |
---|
568 | | - if (gpio_is_valid(spi->cs_gpio)) { |
---|
569 | | - gpio_set_value(spi->cs_gpio, on); |
---|
| 555 | + if (spi->cs_gpiod) { |
---|
| 556 | + gpiod_set_value(spi->cs_gpiod, on); |
---|
570 | 557 | } else if (cs < mpc8xxx_spi->native_chipselects) { |
---|
571 | 558 | slvsel = mpc8xxx_spi_read_reg(®_base->slvsel); |
---|
572 | 559 | slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs)); |
---|
.. | .. |
---|
579 | 566 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
---|
580 | 567 | struct spi_master *master = dev_get_drvdata(dev); |
---|
581 | 568 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
---|
582 | | - struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base; |
---|
| 569 | + struct fsl_spi_reg __iomem *reg_base = mpc8xxx_spi->reg_base; |
---|
583 | 570 | int mbits; |
---|
584 | 571 | u32 capabilities; |
---|
585 | 572 | |
---|
.. | .. |
---|
599 | 586 | pdata->cs_control = fsl_spi_grlib_cs_control; |
---|
600 | 587 | } |
---|
601 | 588 | |
---|
602 | | -static struct spi_master * fsl_spi_probe(struct device *dev, |
---|
| 589 | +static struct spi_master *fsl_spi_probe(struct device *dev, |
---|
603 | 590 | struct resource *mem, unsigned int irq) |
---|
604 | 591 | { |
---|
605 | 592 | struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
---|
606 | 593 | struct spi_master *master; |
---|
607 | 594 | struct mpc8xxx_spi *mpc8xxx_spi; |
---|
608 | | - struct fsl_spi_reg *reg_base; |
---|
| 595 | + struct fsl_spi_reg __iomem *reg_base; |
---|
609 | 596 | u32 regval; |
---|
610 | 597 | int ret = 0; |
---|
611 | 598 | |
---|
.. | .. |
---|
622 | 609 | master->setup = fsl_spi_setup; |
---|
623 | 610 | master->cleanup = fsl_spi_cleanup; |
---|
624 | 611 | master->transfer_one_message = fsl_spi_do_one_msg; |
---|
| 612 | + master->use_gpio_descriptors = true; |
---|
625 | 613 | |
---|
626 | 614 | mpc8xxx_spi = spi_master_get_devdata(master); |
---|
627 | 615 | mpc8xxx_spi->max_bits_per_word = 32; |
---|
.. | .. |
---|
640 | 628 | if (mpc8xxx_spi->type == TYPE_GRLIB) |
---|
641 | 629 | fsl_spi_grlib_probe(dev); |
---|
642 | 630 | |
---|
643 | | - master->bits_per_word_mask = |
---|
644 | | - (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) & |
---|
| 631 | + if (mpc8xxx_spi->flags & SPI_CPM_MODE) |
---|
| 632 | + master->bits_per_word_mask = |
---|
| 633 | + (SPI_BPW_RANGE_MASK(4, 8) | SPI_BPW_MASK(16) | SPI_BPW_MASK(32)); |
---|
| 634 | + else |
---|
| 635 | + master->bits_per_word_mask = |
---|
| 636 | + (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)); |
---|
| 637 | + |
---|
| 638 | + master->bits_per_word_mask &= |
---|
645 | 639 | SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word); |
---|
646 | 640 | |
---|
647 | 641 | if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) |
---|
.. | .. |
---|
697 | 691 | |
---|
698 | 692 | static void fsl_spi_cs_control(struct spi_device *spi, bool on) |
---|
699 | 693 | { |
---|
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]; |
---|
| 694 | + if (spi->cs_gpiod) { |
---|
| 695 | + gpiod_set_value(spi->cs_gpiod, on); |
---|
| 696 | + } else { |
---|
| 697 | + struct device *dev = spi->dev.parent->parent; |
---|
| 698 | + struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
---|
| 699 | + struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata); |
---|
706 | 700 | |
---|
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; |
---|
| 701 | + if (WARN_ON_ONCE(!pinfo->immr_spi_cs)) |
---|
| 702 | + return; |
---|
| 703 | + iowrite32be(on ? 0 : SPI_BOOT_SEL_BIT, pinfo->immr_spi_cs); |
---|
727 | 704 | } |
---|
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 | 705 | } |
---|
810 | 706 | |
---|
811 | 707 | static int of_fsl_spi_probe(struct platform_device *ofdev) |
---|
.. | .. |
---|
814 | 710 | struct device_node *np = ofdev->dev.of_node; |
---|
815 | 711 | struct spi_master *master; |
---|
816 | 712 | struct resource mem; |
---|
817 | | - int irq = 0, type; |
---|
818 | | - int ret = -ENOMEM; |
---|
| 713 | + int irq, type; |
---|
| 714 | + int ret; |
---|
| 715 | + bool spisel_boot = false; |
---|
| 716 | +#if IS_ENABLED(CONFIG_FSL_SOC) |
---|
| 717 | + struct mpc8xxx_spi_probe_info *pinfo = NULL; |
---|
| 718 | +#endif |
---|
| 719 | + |
---|
819 | 720 | |
---|
820 | 721 | ret = of_mpc8xxx_spi_probe(ofdev); |
---|
821 | 722 | if (ret) |
---|
.. | .. |
---|
823 | 724 | |
---|
824 | 725 | type = fsl_spi_get_type(&ofdev->dev); |
---|
825 | 726 | if (type == TYPE_FSL) { |
---|
826 | | - ret = of_fsl_spi_get_chipselects(dev); |
---|
827 | | - if (ret) |
---|
828 | | - goto err; |
---|
| 727 | + struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); |
---|
| 728 | +#if IS_ENABLED(CONFIG_FSL_SOC) |
---|
| 729 | + pinfo = to_of_pinfo(pdata); |
---|
| 730 | + |
---|
| 731 | + spisel_boot = of_property_read_bool(np, "fsl,spisel_boot"); |
---|
| 732 | + if (spisel_boot) { |
---|
| 733 | + pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4); |
---|
| 734 | + if (!pinfo->immr_spi_cs) |
---|
| 735 | + return -ENOMEM; |
---|
| 736 | + } |
---|
| 737 | +#endif |
---|
| 738 | + /* |
---|
| 739 | + * Handle the case where we have one hardwired (always selected) |
---|
| 740 | + * device on the first "chipselect". Else we let the core code |
---|
| 741 | + * handle any GPIOs or native chip selects and assign the |
---|
| 742 | + * appropriate callback for dealing with the CS lines. This isn't |
---|
| 743 | + * supported on the GRLIB variant. |
---|
| 744 | + */ |
---|
| 745 | + ret = gpiod_count(dev, "cs"); |
---|
| 746 | + if (ret < 0) |
---|
| 747 | + ret = 0; |
---|
| 748 | + if (ret == 0 && !spisel_boot) { |
---|
| 749 | + pdata->max_chipselect = 1; |
---|
| 750 | + } else { |
---|
| 751 | + pdata->max_chipselect = ret + spisel_boot; |
---|
| 752 | + pdata->cs_control = fsl_spi_cs_control; |
---|
| 753 | + } |
---|
829 | 754 | } |
---|
830 | 755 | |
---|
831 | 756 | ret = of_address_to_resource(np, 0, &mem); |
---|
832 | 757 | if (ret) |
---|
833 | | - goto err; |
---|
| 758 | + goto unmap_out; |
---|
834 | 759 | |
---|
835 | 760 | irq = platform_get_irq(ofdev, 0); |
---|
836 | 761 | if (irq < 0) { |
---|
837 | 762 | ret = irq; |
---|
838 | | - goto err; |
---|
| 763 | + goto unmap_out; |
---|
839 | 764 | } |
---|
840 | 765 | |
---|
841 | 766 | master = fsl_spi_probe(dev, &mem, irq); |
---|
842 | | - if (IS_ERR(master)) { |
---|
843 | | - ret = PTR_ERR(master); |
---|
844 | | - goto err; |
---|
845 | | - } |
---|
846 | 767 | |
---|
847 | | - return 0; |
---|
| 768 | + return PTR_ERR_OR_ZERO(master); |
---|
848 | 769 | |
---|
849 | | -err: |
---|
850 | | - if (type == TYPE_FSL) |
---|
851 | | - of_fsl_spi_free_chipselects(dev); |
---|
| 770 | +unmap_out: |
---|
| 771 | +#if IS_ENABLED(CONFIG_FSL_SOC) |
---|
| 772 | + if (spisel_boot) |
---|
| 773 | + iounmap(pinfo->immr_spi_cs); |
---|
| 774 | +#endif |
---|
852 | 775 | return ret; |
---|
853 | 776 | } |
---|
854 | 777 | |
---|
.. | .. |
---|
858 | 781 | struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); |
---|
859 | 782 | |
---|
860 | 783 | fsl_spi_cpm_free(mpc8xxx_spi); |
---|
861 | | - if (mpc8xxx_spi->type == TYPE_FSL) |
---|
862 | | - of_fsl_spi_free_chipselects(&ofdev->dev); |
---|
863 | 784 | return 0; |
---|
864 | 785 | } |
---|
865 | 786 | |
---|