aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorCyrille Pitchen <cyrille.pitchen@atmel.com>2015-06-09 12:22:19 -0400
committerWolfram Sang <wsa@the-dreams.de>2015-06-10 08:55:36 -0400
commit5e3cfc6c4e37ec7040fa235f42586b3312c0ebf2 (patch)
tree59f03ab3181e5b41dd6a904fa4a62c17f384719c /drivers/i2c
parent6ce461ea504b0c5eb011b1acccd38fdc28e812ad (diff)
i2c: at91: add support to FIFOs
When FIFOs are available and enabled, the driver now configures the Atmel eXtended DMA Controller to perform word accesses instead of byte accesses when possible. The actual access width depends on the size of the buffer to transmit. To enable FIFO support the "atmel,fifo-size" property must be set properly in the I2C controller node of the device tree. Signed-off-by: Cyrille Pitchen <cyrille.pitchen@atmel.com> Acked-by: Ludovic Desroches <ludovic.desroches@atmel.com> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/busses/i2c-at91.c147
1 files changed, 130 insertions, 17 deletions
diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c
index abd93f3a05a0..9e54f97a9020 100644
--- a/drivers/i2c/busses/i2c-at91.c
+++ b/drivers/i2c/busses/i2c-at91.c
@@ -54,6 +54,8 @@
54#define AT91_TWI_THRCLR BIT(24) /* Transmit Holding Register Clear */ 54#define AT91_TWI_THRCLR BIT(24) /* Transmit Holding Register Clear */
55#define AT91_TWI_RHRCLR BIT(25) /* Receive Holding Register Clear */ 55#define AT91_TWI_RHRCLR BIT(25) /* Receive Holding Register Clear */
56#define AT91_TWI_LOCKCLR BIT(26) /* Lock Clear */ 56#define AT91_TWI_LOCKCLR BIT(26) /* Lock Clear */
57#define AT91_TWI_FIFOEN BIT(28) /* FIFO Enable */
58#define AT91_TWI_FIFODIS BIT(29) /* FIFO Disable */
57 59
58#define AT91_TWI_MMR 0x0004 /* Master Mode Register */ 60#define AT91_TWI_MMR 0x0004 /* Master Mode Register */
59#define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */ 61#define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
@@ -85,6 +87,22 @@
85#define AT91_TWI_ACR_DATAL(len) ((len) & 0xff) 87#define AT91_TWI_ACR_DATAL(len) ((len) & 0xff)
86#define AT91_TWI_ACR_DIR BIT(8) 88#define AT91_TWI_ACR_DIR BIT(8)
87 89
90#define AT91_TWI_FMR 0x0050 /* FIFO Mode Register */
91#define AT91_TWI_FMR_TXRDYM(mode) (((mode) & 0x3) << 0)
92#define AT91_TWI_FMR_TXRDYM_MASK (0x3 << 0)
93#define AT91_TWI_FMR_RXRDYM(mode) (((mode) & 0x3) << 4)
94#define AT91_TWI_FMR_RXRDYM_MASK (0x3 << 4)
95#define AT91_TWI_ONE_DATA 0x0
96#define AT91_TWI_TWO_DATA 0x1
97#define AT91_TWI_FOUR_DATA 0x2
98
99#define AT91_TWI_FLR 0x0054 /* FIFO Level Register */
100
101#define AT91_TWI_FSR 0x0060 /* FIFO Status Register */
102#define AT91_TWI_FIER 0x0064 /* FIFO Interrupt Enable Register */
103#define AT91_TWI_FIDR 0x0068 /* FIFO Interrupt Disable Register */
104#define AT91_TWI_FIMR 0x006c /* FIFO Interrupt Mask Register */
105
88#define AT91_TWI_VER 0x00fc /* Version Register */ 106#define AT91_TWI_VER 0x00fc /* Version Register */
89 107
90struct at91_twi_pdata { 108struct at91_twi_pdata {
@@ -98,7 +116,7 @@ struct at91_twi_pdata {
98struct at91_twi_dma { 116struct at91_twi_dma {
99 struct dma_chan *chan_rx; 117 struct dma_chan *chan_rx;
100 struct dma_chan *chan_tx; 118 struct dma_chan *chan_tx;
101 struct scatterlist sg; 119 struct scatterlist sg[2];
102 struct dma_async_tx_descriptor *data_desc; 120 struct dma_async_tx_descriptor *data_desc;
103 enum dma_data_direction direction; 121 enum dma_data_direction direction;
104 bool buf_mapped; 122 bool buf_mapped;
@@ -121,6 +139,7 @@ struct at91_twi_dev {
121 struct at91_twi_pdata *pdata; 139 struct at91_twi_pdata *pdata;
122 bool use_dma; 140 bool use_dma;
123 bool recv_len_abort; 141 bool recv_len_abort;
142 u32 fifo_size;
124 struct at91_twi_dma dma; 143 struct at91_twi_dma dma;
125}; 144};
126 145
@@ -154,6 +173,9 @@ static void at91_init_twi_bus(struct at91_twi_dev *dev)
154{ 173{
155 at91_disable_twi_interrupts(dev); 174 at91_disable_twi_interrupts(dev);
156 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST); 175 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
176 /* FIFO should be enabled immediately after the software reset */
177 if (dev->fifo_size)
178 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
157 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN); 179 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
158 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS); 180 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
159 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg); 181 at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
@@ -200,7 +222,7 @@ static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
200 dma->xfer_in_progress = false; 222 dma->xfer_in_progress = false;
201 } 223 }
202 if (dma->buf_mapped) { 224 if (dma->buf_mapped) {
203 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg), 225 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
204 dev->buf_len, dma->direction); 226 dev->buf_len, dma->direction);
205 dma->buf_mapped = false; 227 dma->buf_mapped = false;
206 } 228 }
@@ -213,7 +235,8 @@ static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
213 if (dev->buf_len <= 0) 235 if (dev->buf_len <= 0)
214 return; 236 return;
215 237
216 at91_twi_write(dev, AT91_TWI_THR, *dev->buf); 238 /* 8bit write works with and without FIFO */
239 writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
217 240
218 /* send stop when last byte has been written */ 241 /* send stop when last byte has been written */
219 if (--dev->buf_len == 0) 242 if (--dev->buf_len == 0)
@@ -229,7 +252,7 @@ static void at91_twi_write_data_dma_callback(void *data)
229{ 252{
230 struct at91_twi_dev *dev = (struct at91_twi_dev *)data; 253 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
231 254
232 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg), 255 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
233 dev->buf_len, DMA_TO_DEVICE); 256 dev->buf_len, DMA_TO_DEVICE);
234 257
235 /* 258 /*
@@ -250,6 +273,7 @@ static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
250 struct dma_async_tx_descriptor *txdesc; 273 struct dma_async_tx_descriptor *txdesc;
251 struct at91_twi_dma *dma = &dev->dma; 274 struct at91_twi_dma *dma = &dev->dma;
252 struct dma_chan *chan_tx = dma->chan_tx; 275 struct dma_chan *chan_tx = dma->chan_tx;
276 unsigned int sg_len = 1;
253 277
254 if (dev->buf_len <= 0) 278 if (dev->buf_len <= 0)
255 return; 279 return;
@@ -265,10 +289,43 @@ static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
265 } 289 }
266 dma->buf_mapped = true; 290 dma->buf_mapped = true;
267 at91_twi_irq_restore(dev); 291 at91_twi_irq_restore(dev);
268 sg_dma_len(&dma->sg) = dev->buf_len;
269 sg_dma_address(&dma->sg) = dma_addr;
270 292
271 txdesc = dmaengine_prep_slave_sg(chan_tx, &dma->sg, 1, DMA_MEM_TO_DEV, 293 if (dev->fifo_size) {
294 size_t part1_len, part2_len;
295 struct scatterlist *sg;
296 unsigned fifo_mr;
297
298 sg_len = 0;
299
300 part1_len = dev->buf_len & ~0x3;
301 if (part1_len) {
302 sg = &dma->sg[sg_len++];
303 sg_dma_len(sg) = part1_len;
304 sg_dma_address(sg) = dma_addr;
305 }
306
307 part2_len = dev->buf_len & 0x3;
308 if (part2_len) {
309 sg = &dma->sg[sg_len++];
310 sg_dma_len(sg) = part2_len;
311 sg_dma_address(sg) = dma_addr + part1_len;
312 }
313
314 /*
315 * DMA controller is triggered when at least 4 data can be
316 * written into the TX FIFO
317 */
318 fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
319 fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
320 fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
321 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
322 } else {
323 sg_dma_len(&dma->sg[0]) = dev->buf_len;
324 sg_dma_address(&dma->sg[0]) = dma_addr;
325 }
326
327 txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
328 DMA_MEM_TO_DEV,
272 DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 329 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
273 if (!txdesc) { 330 if (!txdesc) {
274 dev_err(dev->dev, "dma prep slave sg failed\n"); 331 dev_err(dev->dev, "dma prep slave sg failed\n");
@@ -293,7 +350,8 @@ static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
293 if (dev->buf_len <= 0) 350 if (dev->buf_len <= 0)
294 return; 351 return;
295 352
296 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff; 353 /* 8bit read works with and without FIFO */
354 *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
297 --dev->buf_len; 355 --dev->buf_len;
298 356
299 /* return if aborting, we only needed to read RHR to clear RXRDY*/ 357 /* return if aborting, we only needed to read RHR to clear RXRDY*/
@@ -330,7 +388,7 @@ static void at91_twi_read_data_dma_callback(void *data)
330 struct at91_twi_dev *dev = (struct at91_twi_dev *)data; 388 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
331 unsigned ier = AT91_TWI_TXCOMP; 389 unsigned ier = AT91_TWI_TXCOMP;
332 390
333 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg), 391 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
334 dev->buf_len, DMA_FROM_DEVICE); 392 dev->buf_len, DMA_FROM_DEVICE);
335 393
336 if (!dev->pdata->has_alt_cmd) { 394 if (!dev->pdata->has_alt_cmd) {
@@ -362,10 +420,24 @@ static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
362 } 420 }
363 dma->buf_mapped = true; 421 dma->buf_mapped = true;
364 at91_twi_irq_restore(dev); 422 at91_twi_irq_restore(dev);
365 dma->sg.dma_address = dma_addr;
366 sg_dma_len(&dma->sg) = buf_len;
367 423
368 rxdesc = dmaengine_prep_slave_sg(chan_rx, &dma->sg, 1, DMA_DEV_TO_MEM, 424 if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
425 unsigned fifo_mr;
426
427 /*
428 * DMA controller is triggered when at least 4 data can be
429 * read from the RX FIFO
430 */
431 fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
432 fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
433 fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
434 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
435 }
436
437 sg_dma_len(&dma->sg[0]) = buf_len;
438 sg_dma_address(&dma->sg[0]) = dma_addr;
439
440 rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
369 DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 441 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
370 if (!rxdesc) { 442 if (!rxdesc) {
371 dev_err(dev->dev, "dma prep slave sg failed\n"); 443 dev_err(dev->dev, "dma prep slave sg failed\n");
@@ -465,6 +537,21 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
465 reinit_completion(&dev->cmd_complete); 537 reinit_completion(&dev->cmd_complete);
466 dev->transfer_status = 0; 538 dev->transfer_status = 0;
467 539
540 if (dev->fifo_size) {
541 unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
542
543 /* Reset FIFO mode register */
544 fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
545 AT91_TWI_FMR_RXRDYM_MASK);
546 fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
547 fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
548 at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
549
550 /* Flush FIFOs */
551 at91_twi_write(dev, AT91_TWI_CR,
552 AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
553 }
554
468 if (!dev->buf_len) { 555 if (!dev->buf_len) {
469 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK); 556 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
470 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP); 557 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
@@ -536,7 +623,8 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
536 ret = -EIO; 623 ret = -EIO;
537 goto error; 624 goto error;
538 } 625 }
539 if (has_alt_cmd && (dev->transfer_status & AT91_TWI_LOCK)) { 626 if ((has_alt_cmd || dev->fifo_size) &&
627 (dev->transfer_status & AT91_TWI_LOCK)) {
540 dev_err(dev->dev, "tx locked\n"); 628 dev_err(dev->dev, "tx locked\n");
541 ret = -EIO; 629 ret = -EIO;
542 goto error; 630 goto error;
@@ -555,7 +643,8 @@ error:
555 /* first stop DMA transfer if still in progress */ 643 /* first stop DMA transfer if still in progress */
556 at91_twi_dma_cleanup(dev); 644 at91_twi_dma_cleanup(dev);
557 /* then flush THR/FIFO and unlock TX if locked */ 645 /* then flush THR/FIFO and unlock TX if locked */
558 if (has_alt_cmd && (dev->transfer_status & AT91_TWI_LOCK)) { 646 if ((has_alt_cmd || dev->fifo_size) &&
647 (dev->transfer_status & AT91_TWI_LOCK)) {
559 dev_dbg(dev->dev, "unlock tx\n"); 648 dev_dbg(dev->dev, "unlock tx\n");
560 at91_twi_write(dev, AT91_TWI_CR, 649 at91_twi_write(dev, AT91_TWI_CR,
561 AT91_TWI_THRCLR | AT91_TWI_LOCKCLR); 650 AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
@@ -750,13 +839,32 @@ static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
750 int ret = 0; 839 int ret = 0;
751 struct dma_slave_config slave_config; 840 struct dma_slave_config slave_config;
752 struct at91_twi_dma *dma = &dev->dma; 841 struct at91_twi_dma *dma = &dev->dma;
842 enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
843
844 /*
845 * The actual width of the access will be chosen in
846 * dmaengine_prep_slave_sg():
847 * for each buffer in the scatter-gather list, if its size is aligned
848 * to addr_width then addr_width accesses will be performed to transfer
849 * the buffer. On the other hand, if the buffer size is not aligned to
850 * addr_width then the buffer is transferred using single byte accesses.
851 * Please refer to the Atmel eXtended DMA controller driver.
852 * When FIFOs are used, the TXRDYM threshold can always be set to
853 * trigger the XDMAC when at least 4 data can be written into the TX
854 * FIFO, even if single byte accesses are performed.
855 * However the RXRDYM threshold must be set to fit the access width,
856 * deduced from buffer length, so the XDMAC is triggered properly to
857 * read data from the RX FIFO.
858 */
859 if (dev->fifo_size)
860 addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
753 861
754 memset(&slave_config, 0, sizeof(slave_config)); 862 memset(&slave_config, 0, sizeof(slave_config));
755 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR; 863 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
756 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 864 slave_config.src_addr_width = addr_width;
757 slave_config.src_maxburst = 1; 865 slave_config.src_maxburst = 1;
758 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR; 866 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
759 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 867 slave_config.dst_addr_width = addr_width;
760 slave_config.dst_maxburst = 1; 868 slave_config.dst_maxburst = 1;
761 slave_config.device_fc = false; 869 slave_config.device_fc = false;
762 870
@@ -788,7 +896,7 @@ static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
788 goto error; 896 goto error;
789 } 897 }
790 898
791 sg_init_table(&dma->sg, 1); 899 sg_init_table(dma->sg, 2);
792 dma->buf_mapped = false; 900 dma->buf_mapped = false;
793 dma->xfer_in_progress = false; 901 dma->xfer_in_progress = false;
794 dev->use_dma = true; 902 dev->use_dma = true;
@@ -874,6 +982,11 @@ static int at91_twi_probe(struct platform_device *pdev)
874 return rc; 982 return rc;
875 } 983 }
876 984
985 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
986 &dev->fifo_size)) {
987 dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
988 }
989
877 rc = of_property_read_u32(dev->dev->of_node, "clock-frequency", 990 rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
878 &bus_clk_rate); 991 &bus_clk_rate);
879 if (rc) 992 if (rc)