aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLudovic Desroches <ludovic.desroches@atmel.com>2012-11-23 04:09:04 -0500
committerWolfram Sang <w.sang@pengutronix.de>2012-11-24 14:27:30 -0500
commit60937b2cdbf948ddb84cbf5d728012519ff4b321 (patch)
treeaac3797f2cd6c5a1bb3131dbf95a4732cd99db9d
parent5f433819b3ee4d8603f834e1438462c4ad58b185 (diff)
i2c: at91: add dma support
Add dma support for Atmel TWI which is available on sam9x5 and later. When using dma for reception, you have to read only n-2 bytes. The last two bytes are read manually. Don't doing this should cause to send the STOP command too late and then to get extra data in the receive register. Signed-off-by: Ludovic Desroches <ludovic.desroches@atmel.com> Signed-off-by: Wolfram Sang <w.sang@pengutronix.de>
-rw-r--r--drivers/i2c/busses/i2c-at91.c306
1 files changed, 298 insertions, 8 deletions
diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c
index 4f7577f23142..b4575ee4bdf3 100644
--- a/drivers/i2c/busses/i2c-at91.c
+++ b/drivers/i2c/busses/i2c-at91.c
@@ -19,6 +19,8 @@
19 19
20#include <linux/clk.h> 20#include <linux/clk.h>
21#include <linux/completion.h> 21#include <linux/completion.h>
22#include <linux/dma-mapping.h>
23#include <linux/dmaengine.h>
22#include <linux/err.h> 24#include <linux/err.h>
23#include <linux/i2c.h> 25#include <linux/i2c.h>
24#include <linux/interrupt.h> 26#include <linux/interrupt.h>
@@ -29,9 +31,11 @@
29#include <linux/of_i2c.h> 31#include <linux/of_i2c.h>
30#include <linux/platform_device.h> 32#include <linux/platform_device.h>
31#include <linux/slab.h> 33#include <linux/slab.h>
34#include <linux/platform_data/dma-atmel.h>
32 35
33#define TWI_CLK_HZ 100000 /* max 400 Kbits/s */ 36#define TWI_CLK_HZ 100000 /* max 400 Kbits/s */
34#define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */ 37#define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */
38#define AT91_I2C_DMA_THRESHOLD 8 /* enable DMA if transfer size is bigger than this threshold */
35 39
36/* AT91 TWI register definitions */ 40/* AT91 TWI register definitions */
37#define AT91_TWI_CR 0x0000 /* Control Register */ 41#define AT91_TWI_CR 0x0000 /* Control Register */
@@ -69,6 +73,18 @@ struct at91_twi_pdata {
69 unsigned clk_max_div; 73 unsigned clk_max_div;
70 unsigned clk_offset; 74 unsigned clk_offset;
71 bool has_unre_flag; 75 bool has_unre_flag;
76 bool has_dma_support;
77 struct at_dma_slave dma_slave;
78};
79
80struct at91_twi_dma {
81 struct dma_chan *chan_rx;
82 struct dma_chan *chan_tx;
83 struct scatterlist sg;
84 struct dma_async_tx_descriptor *data_desc;
85 enum dma_data_direction direction;
86 bool buf_mapped;
87 bool xfer_in_progress;
72}; 88};
73 89
74struct at91_twi_dev { 90struct at91_twi_dev {
@@ -80,10 +96,13 @@ struct at91_twi_dev {
80 size_t buf_len; 96 size_t buf_len;
81 struct i2c_msg *msg; 97 struct i2c_msg *msg;
82 int irq; 98 int irq;
99 unsigned imr;
83 unsigned transfer_status; 100 unsigned transfer_status;
84 struct i2c_adapter adapter; 101 struct i2c_adapter adapter;
85 unsigned twi_cwgr_reg; 102 unsigned twi_cwgr_reg;
86 struct at91_twi_pdata *pdata; 103 struct at91_twi_pdata *pdata;
104 bool use_dma;
105 struct at91_twi_dma dma;
87}; 106};
88 107
89static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg) 108static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
@@ -102,6 +121,17 @@ static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
102 AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY); 121 AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY);
103} 122}
104 123
124static void at91_twi_irq_save(struct at91_twi_dev *dev)
125{
126 dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & 0x7;
127 at91_disable_twi_interrupts(dev);
128}
129
130static void at91_twi_irq_restore(struct at91_twi_dev *dev)
131{
132 at91_twi_write(dev, AT91_TWI_IER, dev->imr);
133}
134
105static void at91_init_twi_bus(struct at91_twi_dev *dev) 135static void at91_init_twi_bus(struct at91_twi_dev *dev)
106{ 136{
107 at91_disable_twi_interrupts(dev); 137 at91_disable_twi_interrupts(dev);
@@ -138,6 +168,28 @@ static void __devinit at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
138 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv); 168 dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
139} 169}
140 170
171static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
172{
173 struct at91_twi_dma *dma = &dev->dma;
174
175 at91_twi_irq_save(dev);
176
177 if (dma->xfer_in_progress) {
178 if (dma->direction == DMA_FROM_DEVICE)
179 dmaengine_terminate_all(dma->chan_rx);
180 else
181 dmaengine_terminate_all(dma->chan_tx);
182 dma->xfer_in_progress = false;
183 }
184 if (dma->buf_mapped) {
185 dma_unmap_single(dev->dev, sg_dma_address(&dma->sg),
186 dev->buf_len, dma->direction);
187 dma->buf_mapped = false;
188 }
189
190 at91_twi_irq_restore(dev);
191}
192
141static void at91_twi_write_next_byte(struct at91_twi_dev *dev) 193static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
142{ 194{
143 if (dev->buf_len <= 0) 195 if (dev->buf_len <= 0)
@@ -154,6 +206,60 @@ static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
154 ++dev->buf; 206 ++dev->buf;
155} 207}
156 208
209static void at91_twi_write_data_dma_callback(void *data)
210{
211 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
212
213 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
214 dev->buf_len, DMA_MEM_TO_DEV);
215
216 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
217}
218
219static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
220{
221 dma_addr_t dma_addr;
222 struct dma_async_tx_descriptor *txdesc;
223 struct at91_twi_dma *dma = &dev->dma;
224 struct dma_chan *chan_tx = dma->chan_tx;
225
226 if (dev->buf_len <= 0)
227 return;
228
229 dma->direction = DMA_TO_DEVICE;
230
231 at91_twi_irq_save(dev);
232 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
233 DMA_TO_DEVICE);
234 if (dma_mapping_error(dev->dev, dma_addr)) {
235 dev_err(dev->dev, "dma map failed\n");
236 return;
237 }
238 dma->buf_mapped = true;
239 at91_twi_irq_restore(dev);
240 sg_dma_len(&dma->sg) = dev->buf_len;
241 sg_dma_address(&dma->sg) = dma_addr;
242
243 txdesc = dmaengine_prep_slave_sg(chan_tx, &dma->sg, 1, DMA_MEM_TO_DEV,
244 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
245 if (!txdesc) {
246 dev_err(dev->dev, "dma prep slave sg failed\n");
247 goto error;
248 }
249
250 txdesc->callback = at91_twi_write_data_dma_callback;
251 txdesc->callback_param = dev;
252
253 dma->xfer_in_progress = true;
254 dmaengine_submit(txdesc);
255 dma_async_issue_pending(chan_tx);
256
257 return;
258
259error:
260 at91_twi_dma_cleanup(dev);
261}
262
157static void at91_twi_read_next_byte(struct at91_twi_dev *dev) 263static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
158{ 264{
159 if (dev->buf_len <= 0) 265 if (dev->buf_len <= 0)
@@ -179,6 +285,61 @@ static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
179 ++dev->buf; 285 ++dev->buf;
180} 286}
181 287
288static void at91_twi_read_data_dma_callback(void *data)
289{
290 struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
291
292 dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg),
293 dev->buf_len, DMA_DEV_TO_MEM);
294
295 /* The last two bytes have to be read without using dma */
296 dev->buf += dev->buf_len - 2;
297 dev->buf_len = 2;
298 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_RXRDY);
299}
300
301static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
302{
303 dma_addr_t dma_addr;
304 struct dma_async_tx_descriptor *rxdesc;
305 struct at91_twi_dma *dma = &dev->dma;
306 struct dma_chan *chan_rx = dma->chan_rx;
307
308 dma->direction = DMA_FROM_DEVICE;
309
310 /* Keep in mind that we won't use dma to read the last two bytes */
311 at91_twi_irq_save(dev);
312 dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len - 2,
313 DMA_FROM_DEVICE);
314 if (dma_mapping_error(dev->dev, dma_addr)) {
315 dev_err(dev->dev, "dma map failed\n");
316 return;
317 }
318 dma->buf_mapped = true;
319 at91_twi_irq_restore(dev);
320 dma->sg.dma_address = dma_addr;
321 sg_dma_len(&dma->sg) = dev->buf_len - 2;
322
323 rxdesc = dmaengine_prep_slave_sg(chan_rx, &dma->sg, 1, DMA_DEV_TO_MEM,
324 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
325 if (!rxdesc) {
326 dev_err(dev->dev, "dma prep slave sg failed\n");
327 goto error;
328 }
329
330 rxdesc->callback = at91_twi_read_data_dma_callback;
331 rxdesc->callback_param = dev;
332
333 dma->xfer_in_progress = true;
334 dmaengine_submit(rxdesc);
335 dma_async_issue_pending(dma->chan_rx);
336
337 return;
338
339error:
340 at91_twi_dma_cleanup(dev);
341}
342
182static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) 343static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
183{ 344{
184 struct at91_twi_dev *dev = dev_id; 345 struct at91_twi_dev *dev = dev_id;
@@ -229,12 +390,36 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
229 if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN)) 390 if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN))
230 start_flags |= AT91_TWI_STOP; 391 start_flags |= AT91_TWI_STOP;
231 at91_twi_write(dev, AT91_TWI_CR, start_flags); 392 at91_twi_write(dev, AT91_TWI_CR, start_flags);
232 at91_twi_write(dev, AT91_TWI_IER, 393 /*
394 * When using dma, the last byte has to be read manually in
395 * order to not send the stop command too late and then
396 * to receive extra data. In practice, there are some issues
397 * if you use the dma to read n-1 bytes because of latency.
398 * Reading n-2 bytes with dma and the two last ones manually
399 * seems to be the best solution.
400 */
401 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
402 at91_twi_read_data_dma(dev);
403 /*
404 * It is important to enable TXCOMP irq here because
405 * doing it only when transferring the last two bytes
406 * will mask NACK errors since TXCOMP is set when a
407 * NACK occurs.
408 */
409 at91_twi_write(dev, AT91_TWI_IER,
410 AT91_TWI_TXCOMP);
411 } else
412 at91_twi_write(dev, AT91_TWI_IER,
233 AT91_TWI_TXCOMP | AT91_TWI_RXRDY); 413 AT91_TWI_TXCOMP | AT91_TWI_RXRDY);
234 } else { 414 } else {
235 at91_twi_write_next_byte(dev); 415 if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
236 at91_twi_write(dev, AT91_TWI_IER, 416 at91_twi_write_data_dma(dev);
237 AT91_TWI_TXCOMP | AT91_TWI_TXRDY); 417 at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
418 } else {
419 at91_twi_write_next_byte(dev);
420 at91_twi_write(dev, AT91_TWI_IER,
421 AT91_TWI_TXCOMP | AT91_TWI_TXRDY);
422 }
238 } 423 }
239 424
240 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, 425 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
@@ -242,23 +427,31 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
242 if (ret == 0) { 427 if (ret == 0) {
243 dev_err(dev->dev, "controller timed out\n"); 428 dev_err(dev->dev, "controller timed out\n");
244 at91_init_twi_bus(dev); 429 at91_init_twi_bus(dev);
245 return -ETIMEDOUT; 430 ret = -ETIMEDOUT;
431 goto error;
246 } 432 }
247 if (dev->transfer_status & AT91_TWI_NACK) { 433 if (dev->transfer_status & AT91_TWI_NACK) {
248 dev_dbg(dev->dev, "received nack\n"); 434 dev_dbg(dev->dev, "received nack\n");
249 return -EREMOTEIO; 435 ret = -EREMOTEIO;
436 goto error;
250 } 437 }
251 if (dev->transfer_status & AT91_TWI_OVRE) { 438 if (dev->transfer_status & AT91_TWI_OVRE) {
252 dev_err(dev->dev, "overrun while reading\n"); 439 dev_err(dev->dev, "overrun while reading\n");
253 return -EIO; 440 ret = -EIO;
441 goto error;
254 } 442 }
255 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) { 443 if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
256 dev_err(dev->dev, "underrun while writing\n"); 444 dev_err(dev->dev, "underrun while writing\n");
257 return -EIO; 445 ret = -EIO;
446 goto error;
258 } 447 }
259 dev_dbg(dev->dev, "transfer complete\n"); 448 dev_dbg(dev->dev, "transfer complete\n");
260 449
261 return 0; 450 return 0;
451
452error:
453 at91_twi_dma_cleanup(dev);
454 return ret;
262} 455}
263 456
264static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num) 457static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
@@ -329,36 +522,42 @@ static struct at91_twi_pdata at91rm9200_config = {
329 .clk_max_div = 5, 522 .clk_max_div = 5,
330 .clk_offset = 3, 523 .clk_offset = 3,
331 .has_unre_flag = true, 524 .has_unre_flag = true,
525 .has_dma_support = false,
332}; 526};
333 527
334static struct at91_twi_pdata at91sam9261_config = { 528static struct at91_twi_pdata at91sam9261_config = {
335 .clk_max_div = 5, 529 .clk_max_div = 5,
336 .clk_offset = 4, 530 .clk_offset = 4,
337 .has_unre_flag = false, 531 .has_unre_flag = false,
532 .has_dma_support = false,
338}; 533};
339 534
340static struct at91_twi_pdata at91sam9260_config = { 535static struct at91_twi_pdata at91sam9260_config = {
341 .clk_max_div = 7, 536 .clk_max_div = 7,
342 .clk_offset = 4, 537 .clk_offset = 4,
343 .has_unre_flag = false, 538 .has_unre_flag = false,
539 .has_dma_support = false,
344}; 540};
345 541
346static struct at91_twi_pdata at91sam9g20_config = { 542static struct at91_twi_pdata at91sam9g20_config = {
347 .clk_max_div = 7, 543 .clk_max_div = 7,
348 .clk_offset = 4, 544 .clk_offset = 4,
349 .has_unre_flag = false, 545 .has_unre_flag = false,
546 .has_dma_support = false,
350}; 547};
351 548
352static struct at91_twi_pdata at91sam9g10_config = { 549static struct at91_twi_pdata at91sam9g10_config = {
353 .clk_max_div = 7, 550 .clk_max_div = 7,
354 .clk_offset = 4, 551 .clk_offset = 4,
355 .has_unre_flag = false, 552 .has_unre_flag = false,
553 .has_dma_support = false,
356}; 554};
357 555
358static struct at91_twi_pdata at91sam9x5_config = { 556static struct at91_twi_pdata at91sam9x5_config = {
359 .clk_max_div = 7, 557 .clk_max_div = 7,
360 .clk_offset = 4, 558 .clk_offset = 4,
361 .has_unre_flag = false, 559 .has_unre_flag = false,
560 .has_dma_support = true,
362}; 561};
363 562
364static const struct platform_device_id at91_twi_devtypes[] = { 563static const struct platform_device_id at91_twi_devtypes[] = {
@@ -405,6 +604,90 @@ MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
405#define atmel_twi_dt_ids NULL 604#define atmel_twi_dt_ids NULL
406#endif 605#endif
407 606
607static bool __devinit filter(struct dma_chan *chan, void *slave)
608{
609 struct at_dma_slave *sl = slave;
610
611 if (sl->dma_dev == chan->device->dev) {
612 chan->private = sl;
613 return true;
614 } else {
615 return false;
616 }
617}
618
619static int __devinit at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
620{
621 int ret = 0;
622 struct at_dma_slave *sdata;
623 struct dma_slave_config slave_config;
624 struct at91_twi_dma *dma = &dev->dma;
625
626 sdata = &dev->pdata->dma_slave;
627
628 memset(&slave_config, 0, sizeof(slave_config));
629 slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
630 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
631 slave_config.src_maxburst = 1;
632 slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
633 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
634 slave_config.dst_maxburst = 1;
635 slave_config.device_fc = false;
636
637 if (sdata && sdata->dma_dev) {
638 dma_cap_mask_t mask;
639
640 dma_cap_zero(mask);
641 dma_cap_set(DMA_SLAVE, mask);
642 dma->chan_tx = dma_request_channel(mask, filter, sdata);
643 if (!dma->chan_tx) {
644 dev_err(dev->dev, "no DMA channel available for tx\n");
645 ret = -EBUSY;
646 goto error;
647 }
648 dma->chan_rx = dma_request_channel(mask, filter, sdata);
649 if (!dma->chan_rx) {
650 dev_err(dev->dev, "no DMA channel available for rx\n");
651 ret = -EBUSY;
652 goto error;
653 }
654 } else {
655 ret = -EINVAL;
656 goto error;
657 }
658
659 slave_config.direction = DMA_MEM_TO_DEV;
660 if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
661 dev_err(dev->dev, "failed to configure tx channel\n");
662 ret = -EINVAL;
663 goto error;
664 }
665
666 slave_config.direction = DMA_DEV_TO_MEM;
667 if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
668 dev_err(dev->dev, "failed to configure rx channel\n");
669 ret = -EINVAL;
670 goto error;
671 }
672
673 sg_init_table(&dma->sg, 1);
674 dma->buf_mapped = false;
675 dma->xfer_in_progress = false;
676
677 dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
678 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
679
680 return ret;
681
682error:
683 dev_info(dev->dev, "can't use DMA\n");
684 if (dma->chan_rx)
685 dma_release_channel(dma->chan_rx);
686 if (dma->chan_tx)
687 dma_release_channel(dma->chan_tx);
688 return ret;
689}
690
408static struct at91_twi_pdata * __devinit at91_twi_get_driver_data( 691static struct at91_twi_pdata * __devinit at91_twi_get_driver_data(
409 struct platform_device *pdev) 692 struct platform_device *pdev)
410{ 693{
@@ -423,6 +706,7 @@ static int __devinit at91_twi_probe(struct platform_device *pdev)
423 struct at91_twi_dev *dev; 706 struct at91_twi_dev *dev;
424 struct resource *mem; 707 struct resource *mem;
425 int rc; 708 int rc;
709 u32 phy_addr;
426 710
427 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 711 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
428 if (!dev) 712 if (!dev)
@@ -433,6 +717,7 @@ static int __devinit at91_twi_probe(struct platform_device *pdev)
433 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 717 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434 if (!mem) 718 if (!mem)
435 return -ENODEV; 719 return -ENODEV;
720 phy_addr = mem->start;
436 721
437 dev->pdata = at91_twi_get_driver_data(pdev); 722 dev->pdata = at91_twi_get_driver_data(pdev);
438 if (!dev->pdata) 723 if (!dev->pdata)
@@ -462,6 +747,11 @@ static int __devinit at91_twi_probe(struct platform_device *pdev)
462 } 747 }
463 clk_prepare_enable(dev->clk); 748 clk_prepare_enable(dev->clk);
464 749
750 if (dev->pdata->has_dma_support) {
751 if (at91_twi_configure_dma(dev, phy_addr) == 0)
752 dev->use_dma = true;
753 }
754
465 at91_calc_twi_clock(dev, TWI_CLK_HZ); 755 at91_calc_twi_clock(dev, TWI_CLK_HZ);
466 at91_init_twi_bus(dev); 756 at91_init_twi_bus(dev);
467 757