Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

atmel_spi: don't always deselect chip between messages

Update chipselect handling for atmel_spi:

* Teach it how to leave chipselect active between messages; this
helps various drivers work better.

* Cope with at91rm0200 errata: nCS0 can't be managed with GPIOs.
The MR.PCS value is now updated whenever a chipselect changes.
(This requires SPI pinmux init for that controller to change,
and also testing on rm9200; doesn't break at91sam9 or avr32.)

* Fix minor glitches: spi_setup() must leave chipselects inactive,
as must removal of the spi_device.

Also tweak diagnostic messaging to be a bit more useful.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Acked-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

David Brownell and committed by
Linus Torvalds
defbd3b4 8da0859a

+103 -24
+103 -24
drivers/spi/atmel_spi.c
··· 46 46 struct clk *clk; 47 47 struct platform_device *pdev; 48 48 unsigned new_1:1; 49 + struct spi_device *stay; 49 50 50 51 u8 stopping; 51 52 struct list_head queue; ··· 63 62 /* 64 63 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby 65 64 * they assume that spi slave device state will not change on deselect, so 66 - * that automagic deselection is OK. Not so! Workaround uses nCSx pins 67 - * as GPIOs; or newer controllers have CSAAT and friends. 65 + * that automagic deselection is OK. ("NPCSx rises if no data is to be 66 + * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer 67 + * controllers have CSAAT and friends. 68 68 * 69 - * Since the CSAAT functionality is a bit weird on newer controllers 70 - * as well, we use GPIO to control nCSx pins on all controllers. 69 + * Since the CSAAT functionality is a bit weird on newer controllers as 70 + * well, we use GPIO to control nCSx pins on all controllers, updating 71 + * MR.PCS to avoid confusing the controller. Using GPIOs also lets us 72 + * support active-high chipselects despite the controller's belief that 73 + * only active-low devices/systems exists. 74 + * 75 + * However, at91rm9200 has a second erratum whereby nCS0 doesn't work 76 + * right when driven with GPIO. ("Mode Fault does not allow more than one 77 + * Master on Chip Select 0.") No workaround exists for that ... so for 78 + * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, 79 + * and (c) will trigger that first erratum in some cases. 71 80 */ 72 81 73 - static inline void cs_activate(struct spi_device *spi) 82 + static void cs_activate(struct atmel_spi *as, struct spi_device *spi) 74 83 { 75 84 unsigned gpio = (unsigned) spi->controller_data; 76 85 unsigned active = spi->mode & SPI_CS_HIGH; 86 + u32 mr; 77 87 78 - dev_dbg(&spi->dev, "activate %u%s\n", gpio, active ? " (high)" : ""); 79 - gpio_set_value(gpio, active); 88 + mr = spi_readl(as, MR); 89 + mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr); 90 + 91 + dev_dbg(&spi->dev, "activate %u%s, mr %08x\n", 92 + gpio, active ? " (high)" : "", 93 + mr); 94 + 95 + if (!(cpu_is_at91rm9200() && spi->chip_select == 0)) 96 + gpio_set_value(gpio, active); 97 + spi_writel(as, MR, mr); 80 98 } 81 99 82 - static inline void cs_deactivate(struct spi_device *spi) 100 + static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) 83 101 { 84 102 unsigned gpio = (unsigned) spi->controller_data; 85 103 unsigned active = spi->mode & SPI_CS_HIGH; 104 + u32 mr; 86 105 87 - dev_dbg(&spi->dev, "DEactivate %u%s\n", gpio, active ? " (low)" : ""); 88 - gpio_set_value(gpio, !active); 106 + /* only deactivate *this* device; sometimes transfers to 107 + * another device may be active when this routine is called. 108 + */ 109 + mr = spi_readl(as, MR); 110 + if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) { 111 + mr = SPI_BFINS(PCS, 0xf, mr); 112 + spi_writel(as, MR, mr); 113 + } 114 + 115 + dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n", 116 + gpio, active ? " (low)" : "", 117 + mr); 118 + 119 + if (!(cpu_is_at91rm9200() && spi->chip_select == 0)) 120 + gpio_set_value(gpio, !active); 89 121 } 90 122 91 123 /* ··· 174 140 175 141 /* REVISIT: when xfer->delay_usecs == 0, the PDC "next transfer" 176 142 * mechanism might help avoid the IRQ latency between transfers 143 + * (and improve the nCS0 errata handling on at91rm9200 chips) 177 144 * 178 145 * We're also waiting for ENDRX before we start the next 179 146 * transfer because we need to handle some difficult timing ··· 204 169 { 205 170 struct atmel_spi *as = spi_master_get_devdata(master); 206 171 struct spi_message *msg; 207 - u32 mr; 172 + struct spi_device *spi; 208 173 209 174 BUG_ON(as->current_transfer); 210 175 211 176 msg = list_entry(as->queue.next, struct spi_message, queue); 177 + spi = msg->spi; 212 178 213 - /* Select the chip */ 214 - mr = spi_readl(as, MR); 215 - mr = SPI_BFINS(PCS, ~(1 << msg->spi->chip_select), mr); 216 - spi_writel(as, MR, mr); 217 - cs_activate(msg->spi); 179 + dev_dbg(master->cdev.dev, "start message %p for %s\n", 180 + msg, spi->dev.bus_id); 181 + 182 + /* select chip if it's not still active */ 183 + if (as->stay) { 184 + if (as->stay != spi) { 185 + cs_deactivate(as, as->stay); 186 + cs_activate(as, spi); 187 + } 188 + as->stay = NULL; 189 + } else 190 + cs_activate(as, spi); 218 191 219 192 atmel_spi_next_xfer(master, msg); 220 193 } ··· 275 232 276 233 static void 277 234 atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as, 278 - struct spi_message *msg, int status) 235 + struct spi_message *msg, int status, int stay) 279 236 { 280 - cs_deactivate(msg->spi); 237 + if (!stay || status < 0) 238 + cs_deactivate(as, msg->spi); 239 + else 240 + as->stay = msg->spi; 241 + 281 242 list_del(&msg->queue); 282 243 msg->status = status; 283 244 ··· 371 324 /* Clear any overrun happening while cleaning up */ 372 325 spi_readl(as, SR); 373 326 374 - atmel_spi_msg_done(master, as, msg, -EIO); 327 + atmel_spi_msg_done(master, as, msg, -EIO, 0); 375 328 } else if (pending & SPI_BIT(ENDRX)) { 376 329 ret = IRQ_HANDLED; 377 330 ··· 389 342 390 343 if (msg->transfers.prev == &xfer->transfer_list) { 391 344 /* report completed message */ 392 - atmel_spi_msg_done(master, as, msg, 0); 345 + atmel_spi_msg_done(master, as, msg, 0, 346 + xfer->cs_change); 393 347 } else { 394 348 if (xfer->cs_change) { 395 - cs_deactivate(msg->spi); 349 + cs_deactivate(as, msg->spi); 396 350 udelay(1); 397 - cs_activate(msg->spi); 351 + cs_activate(as, msg->spi); 398 352 } 399 353 400 354 /* ··· 458 410 return -EINVAL; 459 411 } 460 412 413 + /* see notes above re chipselect */ 414 + if (cpu_is_at91rm9200() 415 + && spi->chip_select == 0 416 + && (spi->mode & SPI_CS_HIGH)) { 417 + dev_dbg(&spi->dev, "setup: can't be active-high\n"); 418 + return -EINVAL; 419 + } 420 + 461 421 /* speed zero convention is used by some upper layers */ 462 422 bus_hz = clk_get_rate(as->clk); 463 423 if (spi->max_speed_hz) { ··· 502 446 return ret; 503 447 spi->controller_state = (void *)npcs_pin; 504 448 gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH)); 449 + } else { 450 + unsigned long flags; 451 + 452 + spin_lock_irqsave(&as->lock, flags); 453 + if (as->stay == spi) 454 + as->stay = NULL; 455 + cs_deactivate(as, spi); 456 + spin_unlock_irqrestore(&as->lock, flags); 505 457 } 506 458 507 459 dev_dbg(&spi->dev, ··· 566 502 } 567 503 } 568 504 505 + #ifdef VERBOSE 569 506 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 570 507 dev_dbg(controller, 571 508 " xfer %p: len %u tx %p/%08x rx %p/%08x\n", ··· 574 509 xfer->tx_buf, xfer->tx_dma, 575 510 xfer->rx_buf, xfer->rx_dma); 576 511 } 512 + #endif 577 513 578 514 msg->status = -EINPROGRESS; 579 515 msg->actual_length = 0; ··· 590 524 591 525 static void atmel_spi_cleanup(struct spi_device *spi) 592 526 { 593 - if (spi->controller_state) 594 - gpio_free((unsigned int)spi->controller_data); 527 + struct atmel_spi *as = spi_master_get_devdata(spi->master); 528 + unsigned gpio = (unsigned) spi->controller_data; 529 + unsigned long flags; 530 + 531 + if (!spi->controller_state) 532 + return; 533 + 534 + spin_lock_irqsave(&as->lock, flags); 535 + if (as->stay == spi) { 536 + as->stay = NULL; 537 + cs_deactivate(as, spi); 538 + } 539 + spin_unlock_irqrestore(&as->lock, flags); 540 + 541 + gpio_free(gpio); 595 542 } 596 543 597 544 /*-------------------------------------------------------------------------*/