xref: /openbmc/linux/drivers/spi/spi-bcm2835aux.c (revision 8102d64c)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21ea29b39SMartin Sperl /*
31ea29b39SMartin Sperl  * Driver for Broadcom BCM2835 auxiliary SPI Controllers
41ea29b39SMartin Sperl  *
51ea29b39SMartin Sperl  * the driver does not rely on the native chipselects at all
61ea29b39SMartin Sperl  * but only uses the gpio type chipselects
71ea29b39SMartin Sperl  *
81ea29b39SMartin Sperl  * Based on: spi-bcm2835.c
91ea29b39SMartin Sperl  *
101ea29b39SMartin Sperl  * Copyright (C) 2015 Martin Sperl
111ea29b39SMartin Sperl  */
121ea29b39SMartin Sperl 
131ea29b39SMartin Sperl #include <linux/clk.h>
141ea29b39SMartin Sperl #include <linux/completion.h>
158048d151SMartin Sperl #include <linux/debugfs.h>
161ea29b39SMartin Sperl #include <linux/delay.h>
171ea29b39SMartin Sperl #include <linux/err.h>
181ea29b39SMartin Sperl #include <linux/interrupt.h>
191ea29b39SMartin Sperl #include <linux/io.h>
201ea29b39SMartin Sperl #include <linux/kernel.h>
211ea29b39SMartin Sperl #include <linux/module.h>
221ea29b39SMartin Sperl #include <linux/of.h>
23749396cbSRob Herring #include <linux/platform_device.h>
241ea29b39SMartin Sperl #include <linux/regmap.h>
251ea29b39SMartin Sperl #include <linux/spi/spi.h>
261ea29b39SMartin Sperl #include <linux/spinlock.h>
271ea29b39SMartin Sperl 
285fd917afSMartin Sperl /* define polling limits */
291a8fa516Skbuild test robot static unsigned int polling_limit_us = 30;
305fd917afSMartin Sperl module_param(polling_limit_us, uint, 0664);
315fd917afSMartin Sperl MODULE_PARM_DESC(polling_limit_us,
325fd917afSMartin Sperl 		 "time in us to run a transfer in polling mode - if zero no polling is used\n");
335fd917afSMartin Sperl 
341ea29b39SMartin Sperl /*
351ea29b39SMartin Sperl  * spi register defines
361ea29b39SMartin Sperl  *
371ea29b39SMartin Sperl  * note there is garbage in the "official" documentation,
381ea29b39SMartin Sperl  * so some data is taken from the file:
391ea29b39SMartin Sperl  *   brcm_usrlib/dag/vmcsx/vcinclude/bcm2708_chip/aux_io.h
401ea29b39SMartin Sperl  * inside of:
411ea29b39SMartin Sperl  *   http://www.broadcom.com/docs/support/videocore/Brcm_Android_ICS_Graphics_Stack.tar.gz
421ea29b39SMartin Sperl  */
431ea29b39SMartin Sperl 
441ea29b39SMartin Sperl /* SPI register offsets */
451ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0	0x00
461ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL1	0x04
471ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT	0x08
481ea29b39SMartin Sperl #define BCM2835_AUX_SPI_PEEK	0x0C
491ea29b39SMartin Sperl #define BCM2835_AUX_SPI_IO	0x20
501ea29b39SMartin Sperl #define BCM2835_AUX_SPI_TXHOLD	0x30
511ea29b39SMartin Sperl 
521ea29b39SMartin Sperl /* Bitfields in CNTL0 */
531ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_SPEED	0xFFF00000
541ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_SPEED_MAX	0xFFF
551ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT	20
561ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_CS	0x000E0000
571ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_POSTINPUT	0x00010000
581ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_VAR_CS	0x00008000
591ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_VAR_WIDTH	0x00004000
601ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_DOUTHOLD	0x00003000
611ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_ENABLE	0x00000800
62e9dd4edcSStephan Olbrich #define BCM2835_AUX_SPI_CNTL0_IN_RISING	0x00000400
631ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_CLEARFIFO	0x00000200
64e9dd4edcSStephan Olbrich #define BCM2835_AUX_SPI_CNTL0_OUT_RISING	0x00000100
651ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_CPOL	0x00000080
661ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_MSBF_OUT	0x00000040
671ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL0_SHIFTLEN	0x0000003F
681ea29b39SMartin Sperl 
691ea29b39SMartin Sperl /* Bitfields in CNTL1 */
701ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL1_CSHIGH	0x00000700
71fe0e2304SStephan Olbrich #define BCM2835_AUX_SPI_CNTL1_TXEMPTY	0x00000080
72fe0e2304SStephan Olbrich #define BCM2835_AUX_SPI_CNTL1_IDLE	0x00000040
731ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL1_MSBF_IN	0x00000002
741ea29b39SMartin Sperl #define BCM2835_AUX_SPI_CNTL1_KEEP_IN	0x00000001
751ea29b39SMartin Sperl 
761ea29b39SMartin Sperl /* Bitfields in STAT */
771ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_TX_LVL	0xFF000000
781ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_RX_LVL	0x00FF0000
791ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_TX_FULL	0x00000400
801ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_TX_EMPTY	0x00000200
811ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_RX_FULL	0x00000100
821ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_RX_EMPTY	0x00000080
831ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_BUSY	0x00000040
841ea29b39SMartin Sperl #define BCM2835_AUX_SPI_STAT_BITCOUNT	0x0000003F
851ea29b39SMartin Sperl 
861ea29b39SMartin Sperl struct bcm2835aux_spi {
871ea29b39SMartin Sperl 	void __iomem *regs;
881ea29b39SMartin Sperl 	struct clk *clk;
891ea29b39SMartin Sperl 	int irq;
901ea29b39SMartin Sperl 	u32 cntl[2];
911ea29b39SMartin Sperl 	const u8 *tx_buf;
921ea29b39SMartin Sperl 	u8 *rx_buf;
931ea29b39SMartin Sperl 	int tx_len;
941ea29b39SMartin Sperl 	int rx_len;
9572aac02bSMartin Sperl 	int pending;
968048d151SMartin Sperl 
978048d151SMartin Sperl 	u64 count_transfer_polling;
988048d151SMartin Sperl 	u64 count_transfer_irq;
998048d151SMartin Sperl 	u64 count_transfer_irq_after_poll;
1008048d151SMartin Sperl 
1018048d151SMartin Sperl 	struct dentry *debugfs_dir;
1021ea29b39SMartin Sperl };
1031ea29b39SMartin Sperl 
1048048d151SMartin Sperl #if defined(CONFIG_DEBUG_FS)
bcm2835aux_debugfs_create(struct bcm2835aux_spi * bs,const char * dname)1058048d151SMartin Sperl static void bcm2835aux_debugfs_create(struct bcm2835aux_spi *bs,
1068048d151SMartin Sperl 				      const char *dname)
1078048d151SMartin Sperl {
1088048d151SMartin Sperl 	char name[64];
1098048d151SMartin Sperl 	struct dentry *dir;
1108048d151SMartin Sperl 
1118048d151SMartin Sperl 	/* get full name */
1128048d151SMartin Sperl 	snprintf(name, sizeof(name), "spi-bcm2835aux-%s", dname);
1138048d151SMartin Sperl 
1148048d151SMartin Sperl 	/* the base directory */
1158048d151SMartin Sperl 	dir = debugfs_create_dir(name, NULL);
1168048d151SMartin Sperl 	bs->debugfs_dir = dir;
1178048d151SMartin Sperl 
1188048d151SMartin Sperl 	/* the counters */
1198048d151SMartin Sperl 	debugfs_create_u64("count_transfer_polling", 0444, dir,
1208048d151SMartin Sperl 			   &bs->count_transfer_polling);
1218048d151SMartin Sperl 	debugfs_create_u64("count_transfer_irq", 0444, dir,
1228048d151SMartin Sperl 			   &bs->count_transfer_irq);
1238048d151SMartin Sperl 	debugfs_create_u64("count_transfer_irq_after_poll", 0444, dir,
1248048d151SMartin Sperl 			   &bs->count_transfer_irq_after_poll);
1258048d151SMartin Sperl }
1268048d151SMartin Sperl 
bcm2835aux_debugfs_remove(struct bcm2835aux_spi * bs)1278048d151SMartin Sperl static void bcm2835aux_debugfs_remove(struct bcm2835aux_spi *bs)
1288048d151SMartin Sperl {
1298048d151SMartin Sperl 	debugfs_remove_recursive(bs->debugfs_dir);
1308048d151SMartin Sperl 	bs->debugfs_dir = NULL;
1318048d151SMartin Sperl }
1328048d151SMartin Sperl #else
bcm2835aux_debugfs_create(struct bcm2835aux_spi * bs,const char * dname)1339b186e9aSYueHaibing static void bcm2835aux_debugfs_create(struct bcm2835aux_spi *bs,
1349b186e9aSYueHaibing 				      const char *dname)
1358048d151SMartin Sperl {
1368048d151SMartin Sperl }
1378048d151SMartin Sperl 
bcm2835aux_debugfs_remove(struct bcm2835aux_spi * bs)1388048d151SMartin Sperl static void bcm2835aux_debugfs_remove(struct bcm2835aux_spi *bs)
1398048d151SMartin Sperl {
1408048d151SMartin Sperl }
1418048d151SMartin Sperl #endif /* CONFIG_DEBUG_FS */
1428048d151SMartin Sperl 
bcm2835aux_rd(struct bcm2835aux_spi * bs,unsigned int reg)143b09bff26SJason Wang static inline u32 bcm2835aux_rd(struct bcm2835aux_spi *bs, unsigned int reg)
1441ea29b39SMartin Sperl {
1451ea29b39SMartin Sperl 	return readl(bs->regs + reg);
1461ea29b39SMartin Sperl }
1471ea29b39SMartin Sperl 
bcm2835aux_wr(struct bcm2835aux_spi * bs,unsigned int reg,u32 val)148b09bff26SJason Wang static inline void bcm2835aux_wr(struct bcm2835aux_spi *bs, unsigned int reg,
1491ea29b39SMartin Sperl 				 u32 val)
1501ea29b39SMartin Sperl {
1511ea29b39SMartin Sperl 	writel(val, bs->regs + reg);
1521ea29b39SMartin Sperl }
1531ea29b39SMartin Sperl 
bcm2835aux_rd_fifo(struct bcm2835aux_spi * bs)1541ea29b39SMartin Sperl static inline void bcm2835aux_rd_fifo(struct bcm2835aux_spi *bs)
1551ea29b39SMartin Sperl {
1561ea29b39SMartin Sperl 	u32 data;
1571ea29b39SMartin Sperl 	int count = min(bs->rx_len, 3);
1581ea29b39SMartin Sperl 
1591ea29b39SMartin Sperl 	data = bcm2835aux_rd(bs, BCM2835_AUX_SPI_IO);
1601ea29b39SMartin Sperl 	if (bs->rx_buf) {
16172aac02bSMartin Sperl 		switch (count) {
16272aac02bSMartin Sperl 		case 3:
16372aac02bSMartin Sperl 			*bs->rx_buf++ = (data >> 16) & 0xff;
164df561f66SGustavo A. R. Silva 			fallthrough;
16572aac02bSMartin Sperl 		case 2:
16672aac02bSMartin Sperl 			*bs->rx_buf++ = (data >> 8) & 0xff;
167df561f66SGustavo A. R. Silva 			fallthrough;
16872aac02bSMartin Sperl 		case 1:
16972aac02bSMartin Sperl 			*bs->rx_buf++ = (data >> 0) & 0xff;
17072aac02bSMartin Sperl 			/* fallthrough - no default */
17172aac02bSMartin Sperl 		}
1721ea29b39SMartin Sperl 	}
1731ea29b39SMartin Sperl 	bs->rx_len -= count;
17472aac02bSMartin Sperl 	bs->pending -= count;
1751ea29b39SMartin Sperl }
1761ea29b39SMartin Sperl 
bcm2835aux_wr_fifo(struct bcm2835aux_spi * bs)1771ea29b39SMartin Sperl static inline void bcm2835aux_wr_fifo(struct bcm2835aux_spi *bs)
1781ea29b39SMartin Sperl {
1791ea29b39SMartin Sperl 	u32 data;
1801ea29b39SMartin Sperl 	u8 byte;
1811ea29b39SMartin Sperl 	int count;
1821ea29b39SMartin Sperl 	int i;
1831ea29b39SMartin Sperl 
1841ea29b39SMartin Sperl 	/* gather up to 3 bytes to write to the FIFO */
1851ea29b39SMartin Sperl 	count = min(bs->tx_len, 3);
1861ea29b39SMartin Sperl 	data = 0;
1871ea29b39SMartin Sperl 	for (i = 0; i < count; i++) {
1881ea29b39SMartin Sperl 		byte = bs->tx_buf ? *bs->tx_buf++ : 0;
1891ea29b39SMartin Sperl 		data |= byte << (8 * (2 - i));
1901ea29b39SMartin Sperl 	}
1911ea29b39SMartin Sperl 
1921ea29b39SMartin Sperl 	/* and set the variable bit-length */
1931ea29b39SMartin Sperl 	data |= (count * 8) << 24;
1941ea29b39SMartin Sperl 
1951ea29b39SMartin Sperl 	/* and decrement length */
1961ea29b39SMartin Sperl 	bs->tx_len -= count;
19772aac02bSMartin Sperl 	bs->pending += count;
1981ea29b39SMartin Sperl 
1991ea29b39SMartin Sperl 	/* write to the correct TX-register */
2001ea29b39SMartin Sperl 	if (bs->tx_len)
2011ea29b39SMartin Sperl 		bcm2835aux_wr(bs, BCM2835_AUX_SPI_TXHOLD, data);
2021ea29b39SMartin Sperl 	else
2031ea29b39SMartin Sperl 		bcm2835aux_wr(bs, BCM2835_AUX_SPI_IO, data);
2041ea29b39SMartin Sperl }
2051ea29b39SMartin Sperl 
bcm2835aux_spi_reset_hw(struct bcm2835aux_spi * bs)2061ea29b39SMartin Sperl static void bcm2835aux_spi_reset_hw(struct bcm2835aux_spi *bs)
2071ea29b39SMartin Sperl {
2081ea29b39SMartin Sperl 	/* disable spi clearing fifo and interrupts */
2091ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, 0);
2101ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0,
2111ea29b39SMartin Sperl 		      BCM2835_AUX_SPI_CNTL0_CLEARFIFO);
2121ea29b39SMartin Sperl }
2131ea29b39SMartin Sperl 
bcm2835aux_spi_transfer_helper(struct bcm2835aux_spi * bs)2147188a6f0SMartin Sperl static void bcm2835aux_spi_transfer_helper(struct bcm2835aux_spi *bs)
2151ea29b39SMartin Sperl {
21673b114eeSMartin Sperl 	u32 stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);
21773b114eeSMartin Sperl 
2181ea29b39SMartin Sperl 	/* check if we have data to read */
21973b114eeSMartin Sperl 	for (; bs->rx_len && (stat & BCM2835_AUX_SPI_STAT_RX_LVL);
22073b114eeSMartin Sperl 	     stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT))
2211ea29b39SMartin Sperl 		bcm2835aux_rd_fifo(bs);
2221ea29b39SMartin Sperl 
2231ea29b39SMartin Sperl 	/* check if we have data to write */
2241ea29b39SMartin Sperl 	while (bs->tx_len &&
22572aac02bSMartin Sperl 	       (bs->pending < 12) &&
2261ea29b39SMartin Sperl 	       (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
2271ea29b39SMartin Sperl 		  BCM2835_AUX_SPI_STAT_TX_FULL))) {
2281ea29b39SMartin Sperl 		bcm2835aux_wr_fifo(bs);
2291ea29b39SMartin Sperl 	}
2307188a6f0SMartin Sperl }
2317188a6f0SMartin Sperl 
bcm2835aux_spi_interrupt(int irq,void * dev_id)2327188a6f0SMartin Sperl static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)
2337188a6f0SMartin Sperl {
2347188a6f0SMartin Sperl 	struct spi_controller *host = dev_id;
2357188a6f0SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
2367188a6f0SMartin Sperl 
2377188a6f0SMartin Sperl 	/* IRQ may be shared, so return if our interrupts are disabled */
2387188a6f0SMartin Sperl 	if (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_CNTL1) &
2397188a6f0SMartin Sperl 	      (BCM2835_AUX_SPI_CNTL1_TXEMPTY | BCM2835_AUX_SPI_CNTL1_IDLE)))
2407188a6f0SMartin Sperl 		return IRQ_NONE;
2417188a6f0SMartin Sperl 
2427188a6f0SMartin Sperl 	/* do common fifo handling */
2437188a6f0SMartin Sperl 	bcm2835aux_spi_transfer_helper(bs);
2441ea29b39SMartin Sperl 
245f29ab184SStephan Olbrich 	if (!bs->tx_len) {
246f29ab184SStephan Olbrich 		/* disable tx fifo empty interrupt */
247f29ab184SStephan Olbrich 		bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
248f29ab184SStephan Olbrich 			BCM2835_AUX_SPI_CNTL1_IDLE);
249f29ab184SStephan Olbrich 	}
250f29ab184SStephan Olbrich 
251b4e2adefSStephan Olbrich 	/* and if rx_len is 0 then disable interrupts and wake up completion */
2521ea29b39SMartin Sperl 	if (!bs->rx_len) {
253b4e2adefSStephan Olbrich 		bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
2547dfa69afSVincent Pelletier 		spi_finalize_current_transfer(host);
2551ea29b39SMartin Sperl 	}
2561ea29b39SMartin Sperl 
2577188a6f0SMartin Sperl 	return IRQ_HANDLED;
2581ea29b39SMartin Sperl }
2591ea29b39SMartin Sperl 
__bcm2835aux_spi_transfer_one_irq(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * tfr)2601ea29b39SMartin Sperl static int __bcm2835aux_spi_transfer_one_irq(struct spi_controller *host,
2611ea29b39SMartin Sperl 					     struct spi_device *spi,
2621ea29b39SMartin Sperl 					     struct spi_transfer *tfr)
2631ea29b39SMartin Sperl {
2641ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
2651ea29b39SMartin Sperl 
2661ea29b39SMartin Sperl 	/* enable interrupts */
2671ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
2681ea29b39SMartin Sperl 		BCM2835_AUX_SPI_CNTL1_TXEMPTY |
2691ea29b39SMartin Sperl 		BCM2835_AUX_SPI_CNTL1_IDLE);
2701ea29b39SMartin Sperl 
2711ea29b39SMartin Sperl 	/* and wait for finish... */
2721ea29b39SMartin Sperl 	return 1;
2731ea29b39SMartin Sperl }
2741ea29b39SMartin Sperl 
bcm2835aux_spi_transfer_one_irq(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * tfr)2751ea29b39SMartin Sperl static int bcm2835aux_spi_transfer_one_irq(struct spi_controller *host,
2761ea29b39SMartin Sperl 					   struct spi_device *spi,
2771ea29b39SMartin Sperl 					   struct spi_transfer *tfr)
2781ea29b39SMartin Sperl {
2791ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
2801ea29b39SMartin Sperl 
2818048d151SMartin Sperl 	/* update statistics */
2828048d151SMartin Sperl 	bs->count_transfer_irq++;
2838048d151SMartin Sperl 
2841ea29b39SMartin Sperl 	/* fill in registers and fifos before enabling interrupts */
2851ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
2861ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
2871ea29b39SMartin Sperl 
2881ea29b39SMartin Sperl 	/* fill in tx fifo with data before enabling interrupts */
2891ea29b39SMartin Sperl 	while ((bs->tx_len) &&
29072aac02bSMartin Sperl 	       (bs->pending < 12) &&
2911ea29b39SMartin Sperl 	       (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
2921ea29b39SMartin Sperl 		  BCM2835_AUX_SPI_STAT_TX_FULL))) {
2931ea29b39SMartin Sperl 		bcm2835aux_wr_fifo(bs);
2941ea29b39SMartin Sperl 	}
2951ea29b39SMartin Sperl 
2961ea29b39SMartin Sperl 	/* now run the interrupt mode */
2971ea29b39SMartin Sperl 	return __bcm2835aux_spi_transfer_one_irq(host, spi, tfr);
2981ea29b39SMartin Sperl }
2991ea29b39SMartin Sperl 
bcm2835aux_spi_transfer_one_poll(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * tfr)3001ea29b39SMartin Sperl static int bcm2835aux_spi_transfer_one_poll(struct spi_controller *host,
3011ea29b39SMartin Sperl 					    struct spi_device *spi,
30272aac02bSMartin Sperl 					struct spi_transfer *tfr)
3031ea29b39SMartin Sperl {
3041ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
3051ea29b39SMartin Sperl 	unsigned long timeout;
3061ea29b39SMartin Sperl 
3078048d151SMartin Sperl 	/* update statistics */
3088048d151SMartin Sperl 	bs->count_transfer_polling++;
3098048d151SMartin Sperl 
3101ea29b39SMartin Sperl 	/* configure spi */
3111ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
3121ea29b39SMartin Sperl 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
3131ea29b39SMartin Sperl 
3145fd917afSMartin Sperl 	/* set the timeout to at least 2 jiffies */
3155fd917afSMartin Sperl 	timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;
3161ea29b39SMartin Sperl 
3171ea29b39SMartin Sperl 	/* loop until finished the transfer */
3181ea29b39SMartin Sperl 	while (bs->rx_len) {
3191ea29b39SMartin Sperl 
3207188a6f0SMartin Sperl 		/* do common fifo handling */
3217188a6f0SMartin Sperl 		bcm2835aux_spi_transfer_helper(bs);
3221ea29b39SMartin Sperl 
3231ea29b39SMartin Sperl 		/* there is still data pending to read check the timeout */
3241ea29b39SMartin Sperl 		if (bs->rx_len && time_after(jiffies, timeout)) {
3251ea29b39SMartin Sperl 			dev_dbg_ratelimited(&spi->dev,
3261ea29b39SMartin Sperl 					    "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
3271ea29b39SMartin Sperl 					    jiffies - timeout,
3281ea29b39SMartin Sperl 					    bs->tx_len, bs->rx_len);
3291ea29b39SMartin Sperl 			/* forward to interrupt handler */
3308048d151SMartin Sperl 			bs->count_transfer_irq_after_poll++;
3311ea29b39SMartin Sperl 			return __bcm2835aux_spi_transfer_one_irq(host,
3321ea29b39SMartin Sperl 							       spi, tfr);
3331ea29b39SMartin Sperl 		}
3341ea29b39SMartin Sperl 	}
3351ea29b39SMartin Sperl 
3361ea29b39SMartin Sperl 	/* and return without waiting for completion */
3371ea29b39SMartin Sperl 	return 0;
3381ea29b39SMartin Sperl }
3391ea29b39SMartin Sperl 
bcm2835aux_spi_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * tfr)3401ea29b39SMartin Sperl static int bcm2835aux_spi_transfer_one(struct spi_controller *host,
3411ea29b39SMartin Sperl 				       struct spi_device *spi,
3421ea29b39SMartin Sperl 				       struct spi_transfer *tfr)
3431ea29b39SMartin Sperl {
3441ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
3455e94c3cdSMartin Sperl 	unsigned long spi_hz, clk_hz, speed;
3465fd917afSMartin Sperl 	unsigned long hz_per_byte, byte_limit;
3471ea29b39SMartin Sperl 
3481ea29b39SMartin Sperl 	/* calculate the registers to handle
3491ea29b39SMartin Sperl 	 *
3501ea29b39SMartin Sperl 	 * note that we use the variable data mode, which
3511ea29b39SMartin Sperl 	 * is not optimal for longer transfers as we waste registers
3521ea29b39SMartin Sperl 	 * resulting (potentially) in more interrupts when transferring
3531ea29b39SMartin Sperl 	 * more than 12 bytes
3541ea29b39SMartin Sperl 	 */
3551ea29b39SMartin Sperl 
3561ea29b39SMartin Sperl 	/* set clock */
3571ea29b39SMartin Sperl 	spi_hz = tfr->speed_hz;
3581ea29b39SMartin Sperl 	clk_hz = clk_get_rate(bs->clk);
3591ea29b39SMartin Sperl 
3601ea29b39SMartin Sperl 	if (spi_hz >= clk_hz / 2) {
3611ea29b39SMartin Sperl 		speed = 0;
3621ea29b39SMartin Sperl 	} else if (spi_hz) {
3631ea29b39SMartin Sperl 		speed = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
3641ea29b39SMartin Sperl 		if (speed >  BCM2835_AUX_SPI_CNTL0_SPEED_MAX)
3651ea29b39SMartin Sperl 			speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
3661ea29b39SMartin Sperl 	} else { /* the slowest we can go */
3671ea29b39SMartin Sperl 		speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
3681ea29b39SMartin Sperl 	}
369b4e2adefSStephan Olbrich 	/* mask out old speed from previous spi_transfer */
370b4e2adefSStephan Olbrich 	bs->cntl[0] &= ~(BCM2835_AUX_SPI_CNTL0_SPEED);
371b4e2adefSStephan Olbrich 	/* set the new speed */
3721ea29b39SMartin Sperl 	bs->cntl[0] |= speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT;
3731ea29b39SMartin Sperl 
3745e94c3cdSMartin Sperl 	tfr->effective_speed_hz = clk_hz / (2 * (speed + 1));
3751ea29b39SMartin Sperl 
3761ea29b39SMartin Sperl 	/* set transmit buffers and length */
3771ea29b39SMartin Sperl 	bs->tx_buf = tfr->tx_buf;
3781ea29b39SMartin Sperl 	bs->rx_buf = tfr->rx_buf;
3791ea29b39SMartin Sperl 	bs->tx_len = tfr->len;
3801ea29b39SMartin Sperl 	bs->rx_len = tfr->len;
38172aac02bSMartin Sperl 	bs->pending = 0;
3821ea29b39SMartin Sperl 
383d704afffSTrent Piepho 	/* Calculate the estimated time in us the transfer runs.  Note that
384db56d030SJay Fang 	 * there are 2 idle clocks cycles after each chunk getting
385d704afffSTrent Piepho 	 * transferred - in our case the chunk size is 3 bytes, so we
386d704afffSTrent Piepho 	 * approximate this by 9 cycles/byte.  This is used to find the number
387d704afffSTrent Piepho 	 * of Hz per byte per polling limit.  E.g., we can transfer 1 byte in
388d704afffSTrent Piepho 	 * 30 µs per 300,000 Hz of bus clock.
38972aac02bSMartin Sperl 	 */
3905fd917afSMartin Sperl 	hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;
3915e94c3cdSMartin Sperl 	byte_limit = hz_per_byte ? tfr->effective_speed_hz / hz_per_byte : 1;
3925fd917afSMartin Sperl 
3931ea29b39SMartin Sperl 	/* run in polling mode for short transfers */
3945fd917afSMartin Sperl 	if (tfr->len < byte_limit)
39572aac02bSMartin Sperl 		return bcm2835aux_spi_transfer_one_poll(host, spi, tfr);
3961ea29b39SMartin Sperl 
3971ea29b39SMartin Sperl 	/* run in interrupt mode for all others */
3981ea29b39SMartin Sperl 	return bcm2835aux_spi_transfer_one_irq(host, spi, tfr);
3991ea29b39SMartin Sperl }
4001ea29b39SMartin Sperl 
bcm2835aux_spi_prepare_message(struct spi_controller * host,struct spi_message * msg)401b4e2adefSStephan Olbrich static int bcm2835aux_spi_prepare_message(struct spi_controller *host,
402b4e2adefSStephan Olbrich 					  struct spi_message *msg)
403b4e2adefSStephan Olbrich {
404b4e2adefSStephan Olbrich 	struct spi_device *spi = msg->spi;
405b4e2adefSStephan Olbrich 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
406b4e2adefSStephan Olbrich 
407b4e2adefSStephan Olbrich 	bs->cntl[0] = BCM2835_AUX_SPI_CNTL0_ENABLE |
408b4e2adefSStephan Olbrich 		      BCM2835_AUX_SPI_CNTL0_VAR_WIDTH |
409b4e2adefSStephan Olbrich 		      BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
410b4e2adefSStephan Olbrich 	bs->cntl[1] = BCM2835_AUX_SPI_CNTL1_MSBF_IN;
411b4e2adefSStephan Olbrich 
412b4e2adefSStephan Olbrich 	/* handle all the modes */
413e9dd4edcSStephan Olbrich 	if (spi->mode & SPI_CPOL) {
414b4e2adefSStephan Olbrich 		bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPOL;
415e9dd4edcSStephan Olbrich 		bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_OUT_RISING;
416e9dd4edcSStephan Olbrich 	} else {
417e9dd4edcSStephan Olbrich 		bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_IN_RISING;
418e9dd4edcSStephan Olbrich 	}
419b4e2adefSStephan Olbrich 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
420b4e2adefSStephan Olbrich 	bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
421b4e2adefSStephan Olbrich 
422b4e2adefSStephan Olbrich 	return 0;
423b4e2adefSStephan Olbrich }
424b4e2adefSStephan Olbrich 
bcm2835aux_spi_unprepare_message(struct spi_controller * host,struct spi_message * msg)425b4e2adefSStephan Olbrich static int bcm2835aux_spi_unprepare_message(struct spi_controller *host,
426b4e2adefSStephan Olbrich 					    struct spi_message *msg)
427b4e2adefSStephan Olbrich {
428b4e2adefSStephan Olbrich 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
429b4e2adefSStephan Olbrich 
430b4e2adefSStephan Olbrich 	bcm2835aux_spi_reset_hw(bs);
431b4e2adefSStephan Olbrich 
432b4e2adefSStephan Olbrich 	return 0;
433b4e2adefSStephan Olbrich }
434b4e2adefSStephan Olbrich 
bcm2835aux_spi_handle_err(struct spi_controller * host,struct spi_message * msg)4351ea29b39SMartin Sperl static void bcm2835aux_spi_handle_err(struct spi_controller *host,
4361ea29b39SMartin Sperl 				      struct spi_message *msg)
4371ea29b39SMartin Sperl {
4381ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
4391ea29b39SMartin Sperl 
4401ea29b39SMartin Sperl 	bcm2835aux_spi_reset_hw(bs);
4411ea29b39SMartin Sperl }
4421ea29b39SMartin Sperl 
bcm2835aux_spi_setup(struct spi_device * spi)443519f2c22SMartin Sperl static int bcm2835aux_spi_setup(struct spi_device *spi)
444519f2c22SMartin Sperl {
445519f2c22SMartin Sperl 	/* sanity check for native cs */
446519f2c22SMartin Sperl 	if (spi->mode & SPI_NO_CS)
447519f2c22SMartin Sperl 		return 0;
448ccd978b7SMartin Sperl 
4499e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (spi_get_csgpiod(spi, 0))
450b651d1daSLinus Walleij 		return 0;
451519f2c22SMartin Sperl 
452519f2c22SMartin Sperl 	/* for dt-backwards compatibility: only support native on CS0
453519f2c22SMartin Sperl 	 * known things not supported with broken native CS:
454519f2c22SMartin Sperl 	 * * multiple chip-selects: cs0-cs2 are all
455519f2c22SMartin Sperl 	 *     simultaniously asserted whenever there is a transfer
456519f2c22SMartin Sperl 	 *     this even includes SPI_NO_CS
457519f2c22SMartin Sperl 	 * * SPI_CS_HIGH: cs are always asserted low
458519f2c22SMartin Sperl 	 * * cs_change: cs is deasserted after each spi_transfer
459519f2c22SMartin Sperl 	 * * cs_delay_usec: cs is always deasserted one SCK cycle
460519f2c22SMartin Sperl 	 *     after the last transfer
461519f2c22SMartin Sperl 	 * probably more...
462519f2c22SMartin Sperl 	 */
463519f2c22SMartin Sperl 	dev_warn(&spi->dev,
464519f2c22SMartin Sperl 		 "Native CS is not supported - please configure cs-gpio in device-tree\n");
465519f2c22SMartin Sperl 
4669e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (spi_get_chipselect(spi, 0) == 0)
467519f2c22SMartin Sperl 		return 0;
468519f2c22SMartin Sperl 
469519f2c22SMartin Sperl 	dev_warn(&spi->dev, "Native CS is not working for cs > 0\n");
470519f2c22SMartin Sperl 
471519f2c22SMartin Sperl 	return -EINVAL;
472519f2c22SMartin Sperl }
473519f2c22SMartin Sperl 
bcm2835aux_spi_probe(struct platform_device * pdev)4741ea29b39SMartin Sperl static int bcm2835aux_spi_probe(struct platform_device *pdev)
4751ea29b39SMartin Sperl {
4761ea29b39SMartin Sperl 	struct spi_controller *host;
4771ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs;
4781ea29b39SMartin Sperl 	unsigned long clk_hz;
4791ea29b39SMartin Sperl 	int err;
4801ea29b39SMartin Sperl 
481e13ee6ccSLukas Wunner 	host = devm_spi_alloc_host(&pdev->dev, sizeof(*bs));
482bf93b951SHoan Nguyen An 	if (!host)
4831ea29b39SMartin Sperl 		return -ENOMEM;
4841ea29b39SMartin Sperl 
4851ea29b39SMartin Sperl 	platform_set_drvdata(pdev, host);
486e9dd4edcSStephan Olbrich 	host->mode_bits = (SPI_CPOL | SPI_CS_HIGH | SPI_NO_CS);
4871ea29b39SMartin Sperl 	host->bits_per_word_mask = SPI_BPW_MASK(8);
488509c5836SMartin Sperl 	/* even though the driver never officially supported native CS
489509c5836SMartin Sperl 	 * allow a single native CS for legacy DT support purposes when
490509c5836SMartin Sperl 	 * no cs-gpio is configured.
491509c5836SMartin Sperl 	 * Known limitations for native cs are:
492509c5836SMartin Sperl 	 * * multiple chip-selects: cs0-cs2 are all simultaniously asserted
493509c5836SMartin Sperl 	 *     whenever there is a transfer -  this even includes SPI_NO_CS
494509c5836SMartin Sperl 	 * * SPI_CS_HIGH: is ignores - cs are always asserted low
495509c5836SMartin Sperl 	 * * cs_change: cs is deasserted after each spi_transfer
496509c5836SMartin Sperl 	 * * cs_delay_usec: cs is always deasserted one SCK cycle after
497509c5836SMartin Sperl 	 *     a spi_transfer
498509c5836SMartin Sperl 	 */
499509c5836SMartin Sperl 	host->num_chipselect = 1;
500519f2c22SMartin Sperl 	host->setup = bcm2835aux_spi_setup;
5011ea29b39SMartin Sperl 	host->transfer_one = bcm2835aux_spi_transfer_one;
5021ea29b39SMartin Sperl 	host->handle_err = bcm2835aux_spi_handle_err;
503b4e2adefSStephan Olbrich 	host->prepare_message = bcm2835aux_spi_prepare_message;
504b4e2adefSStephan Olbrich 	host->unprepare_message = bcm2835aux_spi_unprepare_message;
5051ea29b39SMartin Sperl 	host->dev.of_node = pdev->dev.of_node;
506b651d1daSLinus Walleij 	host->use_gpio_descriptors = true;
5071ea29b39SMartin Sperl 
5081ea29b39SMartin Sperl 	bs = spi_controller_get_devdata(host);
5091ea29b39SMartin Sperl 
5101ea29b39SMartin Sperl 	/* the main area */
511d1975d05SYueHaibing 	bs->regs = devm_platform_ioremap_resource(pdev, 0);
512e13ee6ccSLukas Wunner 	if (IS_ERR(bs->regs))
513e13ee6ccSLukas Wunner 		return PTR_ERR(bs->regs);
5141ea29b39SMartin Sperl 
5151ea29b39SMartin Sperl 	bs->clk = devm_clk_get(&pdev->dev, NULL);
516bfc7af6dSYueHaibing 	if (IS_ERR(bs->clk)) {
517d853b340SNathan Chancellor 		err = PTR_ERR(bs->clk);
5181ea29b39SMartin Sperl 		dev_err(&pdev->dev, "could not get clk: %d\n", err);
519d853b340SNathan Chancellor 		return err;
5201ea29b39SMartin Sperl 	}
5211ea29b39SMartin Sperl 
52207bce09eSMartin Sperl 	bs->irq = platform_get_irq(pdev, 0);
523*8102d64cSRuan Jinjie 	if (bs->irq < 0)
524*8102d64cSRuan Jinjie 		return bs->irq;
5251ea29b39SMartin Sperl 
5261ea29b39SMartin Sperl 	/* this also enables the HW block */
5271ea29b39SMartin Sperl 	err = clk_prepare_enable(bs->clk);
5281ea29b39SMartin Sperl 	if (err) {
5291ea29b39SMartin Sperl 		dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
530e13ee6ccSLukas Wunner 		return err;
5311ea29b39SMartin Sperl 	}
5321ea29b39SMartin Sperl 
5331ea29b39SMartin Sperl 	/* just checking if the clock returns a sane value */
5341ea29b39SMartin Sperl 	clk_hz = clk_get_rate(bs->clk);
5351ea29b39SMartin Sperl 	if (!clk_hz) {
5361ea29b39SMartin Sperl 		dev_err(&pdev->dev, "clock returns 0 Hz\n");
5371ea29b39SMartin Sperl 		err = -ENODEV;
5381ea29b39SMartin Sperl 		goto out_clk_disable;
5391ea29b39SMartin Sperl 	}
5401ea29b39SMartin Sperl 
54107bce09eSMartin Sperl 	/* reset SPI-HW block */
54207bce09eSMartin Sperl 	bcm2835aux_spi_reset_hw(bs);
54307bce09eSMartin Sperl 
5441ea29b39SMartin Sperl 	err = devm_request_irq(&pdev->dev, bs->irq,
5451ea29b39SMartin Sperl 			       bcm2835aux_spi_interrupt,
5461ea29b39SMartin Sperl 			       IRQF_SHARED,
5471ea29b39SMartin Sperl 			       dev_name(&pdev->dev), host);
5481ea29b39SMartin Sperl 	if (err) {
5491ea29b39SMartin Sperl 		dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
5501ea29b39SMartin Sperl 		goto out_clk_disable;
5511ea29b39SMartin Sperl 	}
5521ea29b39SMartin Sperl 
553b9dd3f6dSLukas Wunner 	err = spi_register_controller(host);
5541ea29b39SMartin Sperl 	if (err) {
5551ea29b39SMartin Sperl 		dev_err(&pdev->dev, "could not register SPI host: %d\n", err);
5561ea29b39SMartin Sperl 		goto out_clk_disable;
5571ea29b39SMartin Sperl 	}
5581ea29b39SMartin Sperl 
5598048d151SMartin Sperl 	bcm2835aux_debugfs_create(bs, dev_name(&pdev->dev));
5608048d151SMartin Sperl 
5611ea29b39SMartin Sperl 	return 0;
5621ea29b39SMartin Sperl 
5631ea29b39SMartin Sperl out_clk_disable:
5641ea29b39SMartin Sperl 	clk_disable_unprepare(bs->clk);
5651ea29b39SMartin Sperl 	return err;
5661ea29b39SMartin Sperl }
5671ea29b39SMartin Sperl 
bcm2835aux_spi_remove(struct platform_device * pdev)568f3a1c6a0SUwe Kleine-König static void bcm2835aux_spi_remove(struct platform_device *pdev)
5691ea29b39SMartin Sperl {
5701ea29b39SMartin Sperl 	struct spi_controller *host = platform_get_drvdata(pdev);
5711ea29b39SMartin Sperl 	struct bcm2835aux_spi *bs = spi_controller_get_devdata(host);
5721ea29b39SMartin Sperl 
5738048d151SMartin Sperl 	bcm2835aux_debugfs_remove(bs);
5748048d151SMartin Sperl 
575b9dd3f6dSLukas Wunner 	spi_unregister_controller(host);
576b9dd3f6dSLukas Wunner 
5771ea29b39SMartin Sperl 	bcm2835aux_spi_reset_hw(bs);
5781ea29b39SMartin Sperl 
5791ea29b39SMartin Sperl 	/* disable the HW block by releasing the clock */
5801ea29b39SMartin Sperl 	clk_disable_unprepare(bs->clk);
5811ea29b39SMartin Sperl }
5821ea29b39SMartin Sperl 
5831ea29b39SMartin Sperl static const struct of_device_id bcm2835aux_spi_match[] = {
5841ea29b39SMartin Sperl 	{ .compatible = "brcm,bcm2835-aux-spi", },
5851ea29b39SMartin Sperl 	{}
5861ea29b39SMartin Sperl };
5871ea29b39SMartin Sperl MODULE_DEVICE_TABLE(of, bcm2835aux_spi_match);
5881ea29b39SMartin Sperl 
5891ea29b39SMartin Sperl static struct platform_driver bcm2835aux_spi_driver = {
5901ea29b39SMartin Sperl 	.driver		= {
5911ea29b39SMartin Sperl 		.name		= "spi-bcm2835aux",
5921ea29b39SMartin Sperl 		.of_match_table	= bcm2835aux_spi_match,
5931ea29b39SMartin Sperl 	},
5941ea29b39SMartin Sperl 	.probe		= bcm2835aux_spi_probe,
595f3a1c6a0SUwe Kleine-König 	.remove_new	= bcm2835aux_spi_remove,
5961ea29b39SMartin Sperl };
5971ea29b39SMartin Sperl module_platform_driver(bcm2835aux_spi_driver);
5981ea29b39SMartin Sperl 
5991ea29b39SMartin Sperl MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835 aux");
6001ea29b39SMartin Sperl MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
60122bf6cd2SStefan Wahren MODULE_LICENSE("GPL");
602