1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2011 Weinmann Medical GmbH
6*4882a593Smuzhiyun * Author: Nikolaus Voss <n.voss@weinmann.de>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Evolved from original work by:
9*4882a593Smuzhiyun * Copyright (C) 2004 Rick Bronson
10*4882a593Smuzhiyun * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Borrowed heavily from original work by:
13*4882a593Smuzhiyun * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/clk.h>
17*4882a593Smuzhiyun #include <linux/completion.h>
18*4882a593Smuzhiyun #include <linux/dma-mapping.h>
19*4882a593Smuzhiyun #include <linux/dmaengine.h>
20*4882a593Smuzhiyun #include <linux/err.h>
21*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
22*4882a593Smuzhiyun #include <linux/i2c.h>
23*4882a593Smuzhiyun #include <linux/interrupt.h>
24*4882a593Smuzhiyun #include <linux/io.h>
25*4882a593Smuzhiyun #include <linux/of.h>
26*4882a593Smuzhiyun #include <linux/of_device.h>
27*4882a593Smuzhiyun #include <linux/pinctrl/consumer.h>
28*4882a593Smuzhiyun #include <linux/platform_device.h>
29*4882a593Smuzhiyun #include <linux/platform_data/dma-atmel.h>
30*4882a593Smuzhiyun #include <linux/pm_runtime.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #include "i2c-at91.h"
33*4882a593Smuzhiyun
at91_init_twi_bus_master(struct at91_twi_dev * dev)34*4882a593Smuzhiyun void at91_init_twi_bus_master(struct at91_twi_dev *dev)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun struct at91_twi_pdata *pdata = dev->pdata;
37*4882a593Smuzhiyun u32 filtr = 0;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /* FIFO should be enabled immediately after the software reset */
40*4882a593Smuzhiyun if (dev->fifo_size)
41*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
42*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
43*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
44*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* enable digital filter */
47*4882a593Smuzhiyun if (pdata->has_dig_filtr && dev->enable_dig_filt)
48*4882a593Smuzhiyun filtr |= AT91_TWI_FILTR_FILT;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* enable advanced digital filter */
51*4882a593Smuzhiyun if (pdata->has_adv_dig_filtr && dev->enable_dig_filt)
52*4882a593Smuzhiyun filtr |= AT91_TWI_FILTR_FILT |
53*4882a593Smuzhiyun (AT91_TWI_FILTR_THRES(dev->filter_width) &
54*4882a593Smuzhiyun AT91_TWI_FILTR_THRES_MASK);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* enable analog filter */
57*4882a593Smuzhiyun if (pdata->has_ana_filtr && dev->enable_ana_filt)
58*4882a593Smuzhiyun filtr |= AT91_TWI_FILTR_PADFEN;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if (filtr)
61*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_FILTR, filtr);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * Calculate symmetric clock as stated in datasheet:
66*4882a593Smuzhiyun * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
67*4882a593Smuzhiyun */
at91_calc_twi_clock(struct at91_twi_dev * dev)68*4882a593Smuzhiyun static void at91_calc_twi_clock(struct at91_twi_dev *dev)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun int ckdiv, cdiv, div, hold = 0, filter_width = 0;
71*4882a593Smuzhiyun struct at91_twi_pdata *pdata = dev->pdata;
72*4882a593Smuzhiyun int offset = pdata->clk_offset;
73*4882a593Smuzhiyun int max_ckdiv = pdata->clk_max_div;
74*4882a593Smuzhiyun struct i2c_timings timings, *t = &timings;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun i2c_parse_fw_timings(dev->dev, t, true);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
79*4882a593Smuzhiyun 2 * t->bus_freq_hz) - offset);
80*4882a593Smuzhiyun ckdiv = fls(div >> 8);
81*4882a593Smuzhiyun cdiv = div >> ckdiv;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun if (ckdiv > max_ckdiv) {
84*4882a593Smuzhiyun dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
85*4882a593Smuzhiyun ckdiv, max_ckdiv);
86*4882a593Smuzhiyun ckdiv = max_ckdiv;
87*4882a593Smuzhiyun cdiv = 255;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (pdata->has_hold_field) {
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * hold time = HOLD + 3 x T_peripheral_clock
93*4882a593Smuzhiyun * Use clk rate in kHz to prevent overflows when computing
94*4882a593Smuzhiyun * hold.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun hold = DIV_ROUND_UP(t->sda_hold_ns
97*4882a593Smuzhiyun * (clk_get_rate(dev->clk) / 1000), 1000000);
98*4882a593Smuzhiyun hold -= 3;
99*4882a593Smuzhiyun if (hold < 0)
100*4882a593Smuzhiyun hold = 0;
101*4882a593Smuzhiyun if (hold > AT91_TWI_CWGR_HOLD_MAX) {
102*4882a593Smuzhiyun dev_warn(dev->dev,
103*4882a593Smuzhiyun "HOLD field set to its maximum value (%d instead of %d)\n",
104*4882a593Smuzhiyun AT91_TWI_CWGR_HOLD_MAX, hold);
105*4882a593Smuzhiyun hold = AT91_TWI_CWGR_HOLD_MAX;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (pdata->has_adv_dig_filtr) {
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * filter width = 0 to AT91_TWI_FILTR_THRES_MAX
112*4882a593Smuzhiyun * peripheral clocks
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun filter_width = DIV_ROUND_UP(t->digital_filter_width_ns
115*4882a593Smuzhiyun * (clk_get_rate(dev->clk) / 1000), 1000000);
116*4882a593Smuzhiyun if (filter_width > AT91_TWI_FILTR_THRES_MAX) {
117*4882a593Smuzhiyun dev_warn(dev->dev,
118*4882a593Smuzhiyun "Filter threshold set to its maximum value (%d instead of %d)\n",
119*4882a593Smuzhiyun AT91_TWI_FILTR_THRES_MAX, filter_width);
120*4882a593Smuzhiyun filter_width = AT91_TWI_FILTR_THRES_MAX;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv
125*4882a593Smuzhiyun | AT91_TWI_CWGR_HOLD(hold);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun dev->filter_width = filter_width;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun dev_dbg(dev->dev, "cdiv %d ckdiv %d hold %d (%d ns), filter_width %d (%d ns)\n",
130*4882a593Smuzhiyun cdiv, ckdiv, hold, t->sda_hold_ns, filter_width,
131*4882a593Smuzhiyun t->digital_filter_width_ns);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
at91_twi_dma_cleanup(struct at91_twi_dev * dev)134*4882a593Smuzhiyun static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun struct at91_twi_dma *dma = &dev->dma;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun at91_twi_irq_save(dev);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (dma->xfer_in_progress) {
141*4882a593Smuzhiyun if (dma->direction == DMA_FROM_DEVICE)
142*4882a593Smuzhiyun dmaengine_terminate_all(dma->chan_rx);
143*4882a593Smuzhiyun else
144*4882a593Smuzhiyun dmaengine_terminate_all(dma->chan_tx);
145*4882a593Smuzhiyun dma->xfer_in_progress = false;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun if (dma->buf_mapped) {
148*4882a593Smuzhiyun dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
149*4882a593Smuzhiyun dev->buf_len, dma->direction);
150*4882a593Smuzhiyun dma->buf_mapped = false;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun at91_twi_irq_restore(dev);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
at91_twi_write_next_byte(struct at91_twi_dev * dev)156*4882a593Smuzhiyun static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun if (!dev->buf_len)
159*4882a593Smuzhiyun return;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* 8bit write works with and without FIFO */
162*4882a593Smuzhiyun writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* send stop when last byte has been written */
165*4882a593Smuzhiyun if (--dev->buf_len == 0) {
166*4882a593Smuzhiyun if (!dev->use_alt_cmd)
167*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
168*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_TXRDY);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun ++dev->buf;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
at91_twi_write_data_dma_callback(void * data)176*4882a593Smuzhiyun static void at91_twi_write_data_dma_callback(void *data)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
181*4882a593Smuzhiyun dev->buf_len, DMA_TO_DEVICE);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * When this callback is called, THR/TX FIFO is likely not to be empty
185*4882a593Smuzhiyun * yet. So we have to wait for TXCOMP or NACK bits to be set into the
186*4882a593Smuzhiyun * Status Register to be sure that the STOP bit has been sent and the
187*4882a593Smuzhiyun * transfer is completed. The NACK interrupt has already been enabled,
188*4882a593Smuzhiyun * we just have to enable TXCOMP one.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
191*4882a593Smuzhiyun if (!dev->use_alt_cmd)
192*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
at91_twi_write_data_dma(struct at91_twi_dev * dev)195*4882a593Smuzhiyun static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun dma_addr_t dma_addr;
198*4882a593Smuzhiyun struct dma_async_tx_descriptor *txdesc;
199*4882a593Smuzhiyun struct at91_twi_dma *dma = &dev->dma;
200*4882a593Smuzhiyun struct dma_chan *chan_tx = dma->chan_tx;
201*4882a593Smuzhiyun unsigned int sg_len = 1;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (!dev->buf_len)
204*4882a593Smuzhiyun return;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun dma->direction = DMA_TO_DEVICE;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun at91_twi_irq_save(dev);
209*4882a593Smuzhiyun dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
210*4882a593Smuzhiyun DMA_TO_DEVICE);
211*4882a593Smuzhiyun if (dma_mapping_error(dev->dev, dma_addr)) {
212*4882a593Smuzhiyun dev_err(dev->dev, "dma map failed\n");
213*4882a593Smuzhiyun return;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun dma->buf_mapped = true;
216*4882a593Smuzhiyun at91_twi_irq_restore(dev);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun if (dev->fifo_size) {
219*4882a593Smuzhiyun size_t part1_len, part2_len;
220*4882a593Smuzhiyun struct scatterlist *sg;
221*4882a593Smuzhiyun unsigned fifo_mr;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun sg_len = 0;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun part1_len = dev->buf_len & ~0x3;
226*4882a593Smuzhiyun if (part1_len) {
227*4882a593Smuzhiyun sg = &dma->sg[sg_len++];
228*4882a593Smuzhiyun sg_dma_len(sg) = part1_len;
229*4882a593Smuzhiyun sg_dma_address(sg) = dma_addr;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun part2_len = dev->buf_len & 0x3;
233*4882a593Smuzhiyun if (part2_len) {
234*4882a593Smuzhiyun sg = &dma->sg[sg_len++];
235*4882a593Smuzhiyun sg_dma_len(sg) = part2_len;
236*4882a593Smuzhiyun sg_dma_address(sg) = dma_addr + part1_len;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /*
240*4882a593Smuzhiyun * DMA controller is triggered when at least 4 data can be
241*4882a593Smuzhiyun * written into the TX FIFO
242*4882a593Smuzhiyun */
243*4882a593Smuzhiyun fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
244*4882a593Smuzhiyun fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
245*4882a593Smuzhiyun fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
246*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
247*4882a593Smuzhiyun } else {
248*4882a593Smuzhiyun sg_dma_len(&dma->sg[0]) = dev->buf_len;
249*4882a593Smuzhiyun sg_dma_address(&dma->sg[0]) = dma_addr;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
253*4882a593Smuzhiyun DMA_MEM_TO_DEV,
254*4882a593Smuzhiyun DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
255*4882a593Smuzhiyun if (!txdesc) {
256*4882a593Smuzhiyun dev_err(dev->dev, "dma prep slave sg failed\n");
257*4882a593Smuzhiyun goto error;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun txdesc->callback = at91_twi_write_data_dma_callback;
261*4882a593Smuzhiyun txdesc->callback_param = dev;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun dma->xfer_in_progress = true;
264*4882a593Smuzhiyun dmaengine_submit(txdesc);
265*4882a593Smuzhiyun dma_async_issue_pending(chan_tx);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun return;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun error:
270*4882a593Smuzhiyun at91_twi_dma_cleanup(dev);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
at91_twi_read_next_byte(struct at91_twi_dev * dev)273*4882a593Smuzhiyun static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun /*
276*4882a593Smuzhiyun * If we are in this case, it means there is garbage data in RHR, so
277*4882a593Smuzhiyun * delete them.
278*4882a593Smuzhiyun */
279*4882a593Smuzhiyun if (!dev->buf_len) {
280*4882a593Smuzhiyun at91_twi_read(dev, AT91_TWI_RHR);
281*4882a593Smuzhiyun return;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /* 8bit read works with and without FIFO */
285*4882a593Smuzhiyun *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
286*4882a593Smuzhiyun --dev->buf_len;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* return if aborting, we only needed to read RHR to clear RXRDY*/
289*4882a593Smuzhiyun if (dev->recv_len_abort)
290*4882a593Smuzhiyun return;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /* handle I2C_SMBUS_BLOCK_DATA */
293*4882a593Smuzhiyun if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
294*4882a593Smuzhiyun /* ensure length byte is a valid value */
295*4882a593Smuzhiyun if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
296*4882a593Smuzhiyun dev->msg->flags &= ~I2C_M_RECV_LEN;
297*4882a593Smuzhiyun dev->buf_len += *dev->buf;
298*4882a593Smuzhiyun dev->msg->len = dev->buf_len + 1;
299*4882a593Smuzhiyun dev_dbg(dev->dev, "received block length %zu\n",
300*4882a593Smuzhiyun dev->buf_len);
301*4882a593Smuzhiyun } else {
302*4882a593Smuzhiyun /* abort and send the stop by reading one more byte */
303*4882a593Smuzhiyun dev->recv_len_abort = true;
304*4882a593Smuzhiyun dev->buf_len = 1;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun /* send stop if second but last byte has been read */
309*4882a593Smuzhiyun if (!dev->use_alt_cmd && dev->buf_len == 1)
310*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun dev_dbg(dev->dev, "read 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun ++dev->buf;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
at91_twi_read_data_dma_callback(void * data)317*4882a593Smuzhiyun static void at91_twi_read_data_dma_callback(void *data)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
320*4882a593Smuzhiyun unsigned ier = AT91_TWI_TXCOMP;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
323*4882a593Smuzhiyun dev->buf_len, DMA_FROM_DEVICE);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (!dev->use_alt_cmd) {
326*4882a593Smuzhiyun /* The last two bytes have to be read without using dma */
327*4882a593Smuzhiyun dev->buf += dev->buf_len - 2;
328*4882a593Smuzhiyun dev->buf_len = 2;
329*4882a593Smuzhiyun ier |= AT91_TWI_RXRDY;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER, ier);
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
at91_twi_read_data_dma(struct at91_twi_dev * dev)334*4882a593Smuzhiyun static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun dma_addr_t dma_addr;
337*4882a593Smuzhiyun struct dma_async_tx_descriptor *rxdesc;
338*4882a593Smuzhiyun struct at91_twi_dma *dma = &dev->dma;
339*4882a593Smuzhiyun struct dma_chan *chan_rx = dma->chan_rx;
340*4882a593Smuzhiyun size_t buf_len;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun buf_len = (dev->use_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
343*4882a593Smuzhiyun dma->direction = DMA_FROM_DEVICE;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun /* Keep in mind that we won't use dma to read the last two bytes */
346*4882a593Smuzhiyun at91_twi_irq_save(dev);
347*4882a593Smuzhiyun dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
348*4882a593Smuzhiyun if (dma_mapping_error(dev->dev, dma_addr)) {
349*4882a593Smuzhiyun dev_err(dev->dev, "dma map failed\n");
350*4882a593Smuzhiyun return;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun dma->buf_mapped = true;
353*4882a593Smuzhiyun at91_twi_irq_restore(dev);
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
356*4882a593Smuzhiyun unsigned fifo_mr;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun * DMA controller is triggered when at least 4 data can be
360*4882a593Smuzhiyun * read from the RX FIFO
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
363*4882a593Smuzhiyun fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
364*4882a593Smuzhiyun fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
365*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun sg_dma_len(&dma->sg[0]) = buf_len;
369*4882a593Smuzhiyun sg_dma_address(&dma->sg[0]) = dma_addr;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
372*4882a593Smuzhiyun DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
373*4882a593Smuzhiyun if (!rxdesc) {
374*4882a593Smuzhiyun dev_err(dev->dev, "dma prep slave sg failed\n");
375*4882a593Smuzhiyun goto error;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun rxdesc->callback = at91_twi_read_data_dma_callback;
379*4882a593Smuzhiyun rxdesc->callback_param = dev;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun dma->xfer_in_progress = true;
382*4882a593Smuzhiyun dmaengine_submit(rxdesc);
383*4882a593Smuzhiyun dma_async_issue_pending(dma->chan_rx);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun return;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun error:
388*4882a593Smuzhiyun at91_twi_dma_cleanup(dev);
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
atmel_twi_interrupt(int irq,void * dev_id)391*4882a593Smuzhiyun static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun struct at91_twi_dev *dev = dev_id;
394*4882a593Smuzhiyun const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
395*4882a593Smuzhiyun const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun if (!irqstatus)
398*4882a593Smuzhiyun return IRQ_NONE;
399*4882a593Smuzhiyun /*
400*4882a593Smuzhiyun * In reception, the behavior of the twi device (before sama5d2) is
401*4882a593Smuzhiyun * weird. There is some magic about RXRDY flag! When a data has been
402*4882a593Smuzhiyun * almost received, the reception of a new one is anticipated if there
403*4882a593Smuzhiyun * is no stop command to send. That is the reason why ask for sending
404*4882a593Smuzhiyun * the stop command not on the last data but on the second last one.
405*4882a593Smuzhiyun *
406*4882a593Smuzhiyun * Unfortunately, we could still have the RXRDY flag set even if the
407*4882a593Smuzhiyun * transfer is done and we have read the last data. It might happen
408*4882a593Smuzhiyun * when the i2c slave device sends too quickly data after receiving the
409*4882a593Smuzhiyun * ack from the master. The data has been almost received before having
410*4882a593Smuzhiyun * the order to send stop. In this case, sending the stop command could
411*4882a593Smuzhiyun * cause a RXRDY interrupt with a TXCOMP one. It is better to manage
412*4882a593Smuzhiyun * the RXRDY interrupt first in order to not keep garbage data in the
413*4882a593Smuzhiyun * Receive Holding Register for the next transfer.
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun if (irqstatus & AT91_TWI_RXRDY) {
416*4882a593Smuzhiyun /*
417*4882a593Smuzhiyun * Read all available bytes at once by polling RXRDY usable w/
418*4882a593Smuzhiyun * and w/o FIFO. With FIFO enabled we could also read RXFL and
419*4882a593Smuzhiyun * avoid polling RXRDY.
420*4882a593Smuzhiyun */
421*4882a593Smuzhiyun do {
422*4882a593Smuzhiyun at91_twi_read_next_byte(dev);
423*4882a593Smuzhiyun } while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun /*
427*4882a593Smuzhiyun * When a NACK condition is detected, the I2C controller sets the NACK,
428*4882a593Smuzhiyun * TXCOMP and TXRDY bits all together in the Status Register (SR).
429*4882a593Smuzhiyun *
430*4882a593Smuzhiyun * 1 - Handling NACK errors with CPU write transfer.
431*4882a593Smuzhiyun *
432*4882a593Smuzhiyun * In such case, we should not write the next byte into the Transmit
433*4882a593Smuzhiyun * Holding Register (THR) otherwise the I2C controller would start a new
434*4882a593Smuzhiyun * transfer and the I2C slave is likely to reply by another NACK.
435*4882a593Smuzhiyun *
436*4882a593Smuzhiyun * 2 - Handling NACK errors with DMA write transfer.
437*4882a593Smuzhiyun *
438*4882a593Smuzhiyun * By setting the TXRDY bit in the SR, the I2C controller also triggers
439*4882a593Smuzhiyun * the DMA controller to write the next data into the THR. Then the
440*4882a593Smuzhiyun * result depends on the hardware version of the I2C controller.
441*4882a593Smuzhiyun *
442*4882a593Smuzhiyun * 2a - Without support of the Alternative Command mode.
443*4882a593Smuzhiyun *
444*4882a593Smuzhiyun * This is the worst case: the DMA controller is triggered to write the
445*4882a593Smuzhiyun * next data into the THR, hence starting a new transfer: the I2C slave
446*4882a593Smuzhiyun * is likely to reply by another NACK.
447*4882a593Smuzhiyun * Concurrently, this interrupt handler is likely to be called to manage
448*4882a593Smuzhiyun * the first NACK before the I2C controller detects the second NACK and
449*4882a593Smuzhiyun * sets once again the NACK bit into the SR.
450*4882a593Smuzhiyun * When handling the first NACK, this interrupt handler disables the I2C
451*4882a593Smuzhiyun * controller interruptions, especially the NACK interrupt.
452*4882a593Smuzhiyun * Hence, the NACK bit is pending into the SR. This is why we should
453*4882a593Smuzhiyun * read the SR to clear all pending interrupts at the beginning of
454*4882a593Smuzhiyun * at91_do_twi_transfer() before actually starting a new transfer.
455*4882a593Smuzhiyun *
456*4882a593Smuzhiyun * 2b - With support of the Alternative Command mode.
457*4882a593Smuzhiyun *
458*4882a593Smuzhiyun * When a NACK condition is detected, the I2C controller also locks the
459*4882a593Smuzhiyun * THR (and sets the LOCK bit in the SR): even though the DMA controller
460*4882a593Smuzhiyun * is triggered by the TXRDY bit to write the next data into the THR,
461*4882a593Smuzhiyun * this data actually won't go on the I2C bus hence a second NACK is not
462*4882a593Smuzhiyun * generated.
463*4882a593Smuzhiyun */
464*4882a593Smuzhiyun if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
465*4882a593Smuzhiyun at91_disable_twi_interrupts(dev);
466*4882a593Smuzhiyun complete(&dev->cmd_complete);
467*4882a593Smuzhiyun } else if (irqstatus & AT91_TWI_TXRDY) {
468*4882a593Smuzhiyun at91_twi_write_next_byte(dev);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /* catch error flags */
472*4882a593Smuzhiyun dev->transfer_status |= status;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun return IRQ_HANDLED;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
at91_do_twi_transfer(struct at91_twi_dev * dev)477*4882a593Smuzhiyun static int at91_do_twi_transfer(struct at91_twi_dev *dev)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun int ret;
480*4882a593Smuzhiyun unsigned long time_left;
481*4882a593Smuzhiyun bool has_unre_flag = dev->pdata->has_unre_flag;
482*4882a593Smuzhiyun bool has_alt_cmd = dev->pdata->has_alt_cmd;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /*
485*4882a593Smuzhiyun * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
486*4882a593Smuzhiyun * read flag but shows the state of the transmission at the time the
487*4882a593Smuzhiyun * Status Register is read. According to the programmer datasheet,
488*4882a593Smuzhiyun * TXCOMP is set when both holding register and internal shifter are
489*4882a593Smuzhiyun * empty and STOP condition has been sent.
490*4882a593Smuzhiyun * Consequently, we should enable NACK interrupt rather than TXCOMP to
491*4882a593Smuzhiyun * detect transmission failure.
492*4882a593Smuzhiyun * Indeed let's take the case of an i2c write command using DMA.
493*4882a593Smuzhiyun * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
494*4882a593Smuzhiyun * TXCOMP bits are set together into the Status Register.
495*4882a593Smuzhiyun * LOCK is a clear on write bit, which is set to prevent the DMA
496*4882a593Smuzhiyun * controller from sending new data on the i2c bus after a NACK
497*4882a593Smuzhiyun * condition has happened. Once locked, this i2c peripheral stops
498*4882a593Smuzhiyun * triggering the DMA controller for new data but it is more than
499*4882a593Smuzhiyun * likely that a new DMA transaction is already in progress, writing
500*4882a593Smuzhiyun * into the Transmit Holding Register. Since the peripheral is locked,
501*4882a593Smuzhiyun * these new data won't be sent to the i2c bus but they will remain
502*4882a593Smuzhiyun * into the Transmit Holding Register, so TXCOMP bit is cleared.
503*4882a593Smuzhiyun * Then when the interrupt handler is called, the Status Register is
504*4882a593Smuzhiyun * read: the TXCOMP bit is clear but NACK bit is still set. The driver
505*4882a593Smuzhiyun * manage the error properly, without waiting for timeout.
506*4882a593Smuzhiyun * This case can be reproduced easyly when writing into an at24 eeprom.
507*4882a593Smuzhiyun *
508*4882a593Smuzhiyun * Besides, the TXCOMP bit is already set before the i2c transaction
509*4882a593Smuzhiyun * has been started. For read transactions, this bit is cleared when
510*4882a593Smuzhiyun * writing the START bit into the Control Register. So the
511*4882a593Smuzhiyun * corresponding interrupt can safely be enabled just after.
512*4882a593Smuzhiyun * However for write transactions managed by the CPU, we first write
513*4882a593Smuzhiyun * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
514*4882a593Smuzhiyun * interrupt. If TXCOMP interrupt were enabled before writing into THR,
515*4882a593Smuzhiyun * the interrupt handler would be called immediately and the i2c command
516*4882a593Smuzhiyun * would be reported as completed.
517*4882a593Smuzhiyun * Also when a write transaction is managed by the DMA controller,
518*4882a593Smuzhiyun * enabling the TXCOMP interrupt in this function may lead to a race
519*4882a593Smuzhiyun * condition since we don't know whether the TXCOMP interrupt is enabled
520*4882a593Smuzhiyun * before or after the DMA has started to write into THR. So the TXCOMP
521*4882a593Smuzhiyun * interrupt is enabled later by at91_twi_write_data_dma_callback().
522*4882a593Smuzhiyun * Immediately after in that DMA callback, if the alternative command
523*4882a593Smuzhiyun * mode is not used, we still need to send the STOP condition manually
524*4882a593Smuzhiyun * writing the corresponding bit into the Control Register.
525*4882a593Smuzhiyun */
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun dev_dbg(dev->dev, "transfer: %s %zu bytes.\n",
528*4882a593Smuzhiyun (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun reinit_completion(&dev->cmd_complete);
531*4882a593Smuzhiyun dev->transfer_status = 0;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* Clear pending interrupts, such as NACK. */
534*4882a593Smuzhiyun at91_twi_read(dev, AT91_TWI_SR);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (dev->fifo_size) {
537*4882a593Smuzhiyun unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /* Reset FIFO mode register */
540*4882a593Smuzhiyun fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
541*4882a593Smuzhiyun AT91_TWI_FMR_RXRDYM_MASK);
542*4882a593Smuzhiyun fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
543*4882a593Smuzhiyun fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
544*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun /* Flush FIFOs */
547*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR,
548*4882a593Smuzhiyun AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun if (!dev->buf_len) {
552*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
553*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
554*4882a593Smuzhiyun } else if (dev->msg->flags & I2C_M_RD) {
555*4882a593Smuzhiyun unsigned start_flags = AT91_TWI_START;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /* if only one byte is to be read, immediately stop transfer */
558*4882a593Smuzhiyun if (!dev->use_alt_cmd && dev->buf_len <= 1 &&
559*4882a593Smuzhiyun !(dev->msg->flags & I2C_M_RECV_LEN))
560*4882a593Smuzhiyun start_flags |= AT91_TWI_STOP;
561*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, start_flags);
562*4882a593Smuzhiyun /*
563*4882a593Smuzhiyun * When using dma without alternative command mode, the last
564*4882a593Smuzhiyun * byte has to be read manually in order to not send the stop
565*4882a593Smuzhiyun * command too late and then to receive extra data.
566*4882a593Smuzhiyun * In practice, there are some issues if you use the dma to
567*4882a593Smuzhiyun * read n-1 bytes because of latency.
568*4882a593Smuzhiyun * Reading n-2 bytes with dma and the two last ones manually
569*4882a593Smuzhiyun * seems to be the best solution.
570*4882a593Smuzhiyun */
571*4882a593Smuzhiyun if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
572*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
573*4882a593Smuzhiyun at91_twi_read_data_dma(dev);
574*4882a593Smuzhiyun } else {
575*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER,
576*4882a593Smuzhiyun AT91_TWI_TXCOMP |
577*4882a593Smuzhiyun AT91_TWI_NACK |
578*4882a593Smuzhiyun AT91_TWI_RXRDY);
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun } else {
581*4882a593Smuzhiyun if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
582*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
583*4882a593Smuzhiyun at91_twi_write_data_dma(dev);
584*4882a593Smuzhiyun } else {
585*4882a593Smuzhiyun at91_twi_write_next_byte(dev);
586*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IER,
587*4882a593Smuzhiyun AT91_TWI_TXCOMP | AT91_TWI_NACK |
588*4882a593Smuzhiyun (dev->buf_len ? AT91_TWI_TXRDY : 0));
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun time_left = wait_for_completion_timeout(&dev->cmd_complete,
593*4882a593Smuzhiyun dev->adapter.timeout);
594*4882a593Smuzhiyun if (time_left == 0) {
595*4882a593Smuzhiyun dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
596*4882a593Smuzhiyun dev_err(dev->dev, "controller timed out\n");
597*4882a593Smuzhiyun at91_init_twi_bus(dev);
598*4882a593Smuzhiyun ret = -ETIMEDOUT;
599*4882a593Smuzhiyun goto error;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun if (dev->transfer_status & AT91_TWI_NACK) {
602*4882a593Smuzhiyun dev_dbg(dev->dev, "received nack\n");
603*4882a593Smuzhiyun ret = -EREMOTEIO;
604*4882a593Smuzhiyun goto error;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun if (dev->transfer_status & AT91_TWI_OVRE) {
607*4882a593Smuzhiyun dev_err(dev->dev, "overrun while reading\n");
608*4882a593Smuzhiyun ret = -EIO;
609*4882a593Smuzhiyun goto error;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
612*4882a593Smuzhiyun dev_err(dev->dev, "underrun while writing\n");
613*4882a593Smuzhiyun ret = -EIO;
614*4882a593Smuzhiyun goto error;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun if ((has_alt_cmd || dev->fifo_size) &&
617*4882a593Smuzhiyun (dev->transfer_status & AT91_TWI_LOCK)) {
618*4882a593Smuzhiyun dev_err(dev->dev, "tx locked\n");
619*4882a593Smuzhiyun ret = -EIO;
620*4882a593Smuzhiyun goto error;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun if (dev->recv_len_abort) {
623*4882a593Smuzhiyun dev_err(dev->dev, "invalid smbus block length recvd\n");
624*4882a593Smuzhiyun ret = -EPROTO;
625*4882a593Smuzhiyun goto error;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun dev_dbg(dev->dev, "transfer complete\n");
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun return 0;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun error:
633*4882a593Smuzhiyun /* first stop DMA transfer if still in progress */
634*4882a593Smuzhiyun at91_twi_dma_cleanup(dev);
635*4882a593Smuzhiyun /* then flush THR/FIFO and unlock TX if locked */
636*4882a593Smuzhiyun if ((has_alt_cmd || dev->fifo_size) &&
637*4882a593Smuzhiyun (dev->transfer_status & AT91_TWI_LOCK)) {
638*4882a593Smuzhiyun dev_dbg(dev->dev, "unlock tx\n");
639*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR,
640*4882a593Smuzhiyun AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /*
644*4882a593Smuzhiyun * some faulty I2C slave devices might hold SDA down;
645*4882a593Smuzhiyun * we can send a bus clear command, hoping that the pins will be
646*4882a593Smuzhiyun * released
647*4882a593Smuzhiyun */
648*4882a593Smuzhiyun i2c_recover_bus(&dev->adapter);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun return ret;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun
at91_twi_xfer(struct i2c_adapter * adap,struct i2c_msg * msg,int num)653*4882a593Smuzhiyun static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun struct at91_twi_dev *dev = i2c_get_adapdata(adap);
656*4882a593Smuzhiyun int ret;
657*4882a593Smuzhiyun unsigned int_addr_flag = 0;
658*4882a593Smuzhiyun struct i2c_msg *m_start = msg;
659*4882a593Smuzhiyun bool is_read;
660*4882a593Smuzhiyun u8 *dma_buf = NULL;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun ret = pm_runtime_get_sync(dev->dev);
665*4882a593Smuzhiyun if (ret < 0)
666*4882a593Smuzhiyun goto out;
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun if (num == 2) {
669*4882a593Smuzhiyun int internal_address = 0;
670*4882a593Smuzhiyun int i;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /* 1st msg is put into the internal address, start with 2nd */
673*4882a593Smuzhiyun m_start = &msg[1];
674*4882a593Smuzhiyun for (i = 0; i < msg->len; ++i) {
675*4882a593Smuzhiyun const unsigned addr = msg->buf[msg->len - 1 - i];
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun internal_address |= addr << (8 * i);
678*4882a593Smuzhiyun int_addr_flag += AT91_TWI_IADRSZ_1;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_IADR, internal_address);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun dev->use_alt_cmd = false;
684*4882a593Smuzhiyun is_read = (m_start->flags & I2C_M_RD);
685*4882a593Smuzhiyun if (dev->pdata->has_alt_cmd) {
686*4882a593Smuzhiyun if (m_start->len > 0 &&
687*4882a593Smuzhiyun m_start->len < AT91_I2C_MAX_ALT_CMD_DATA_SIZE) {
688*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
689*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_ACR,
690*4882a593Smuzhiyun AT91_TWI_ACR_DATAL(m_start->len) |
691*4882a593Smuzhiyun ((is_read) ? AT91_TWI_ACR_DIR : 0));
692*4882a593Smuzhiyun dev->use_alt_cmd = true;
693*4882a593Smuzhiyun } else {
694*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_MMR,
699*4882a593Smuzhiyun (m_start->addr << 16) |
700*4882a593Smuzhiyun int_addr_flag |
701*4882a593Smuzhiyun ((!dev->use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun dev->buf_len = m_start->len;
704*4882a593Smuzhiyun dev->buf = m_start->buf;
705*4882a593Smuzhiyun dev->msg = m_start;
706*4882a593Smuzhiyun dev->recv_len_abort = false;
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun if (dev->use_dma) {
709*4882a593Smuzhiyun dma_buf = i2c_get_dma_safe_msg_buf(m_start, 1);
710*4882a593Smuzhiyun if (!dma_buf) {
711*4882a593Smuzhiyun ret = -ENOMEM;
712*4882a593Smuzhiyun goto out;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun dev->buf = dma_buf;
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun ret = at91_do_twi_transfer(dev);
718*4882a593Smuzhiyun i2c_put_dma_safe_msg_buf(dma_buf, m_start, !ret);
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun ret = (ret < 0) ? ret : num;
721*4882a593Smuzhiyun out:
722*4882a593Smuzhiyun pm_runtime_mark_last_busy(dev->dev);
723*4882a593Smuzhiyun pm_runtime_put_autosuspend(dev->dev);
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun return ret;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /*
729*4882a593Smuzhiyun * The hardware can handle at most two messages concatenated by a
730*4882a593Smuzhiyun * repeated start via it's internal address feature.
731*4882a593Smuzhiyun */
732*4882a593Smuzhiyun static const struct i2c_adapter_quirks at91_twi_quirks = {
733*4882a593Smuzhiyun .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
734*4882a593Smuzhiyun .max_comb_1st_msg_len = 3,
735*4882a593Smuzhiyun };
736*4882a593Smuzhiyun
at91_twi_func(struct i2c_adapter * adapter)737*4882a593Smuzhiyun static u32 at91_twi_func(struct i2c_adapter *adapter)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
740*4882a593Smuzhiyun | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun static const struct i2c_algorithm at91_twi_algorithm = {
744*4882a593Smuzhiyun .master_xfer = at91_twi_xfer,
745*4882a593Smuzhiyun .functionality = at91_twi_func,
746*4882a593Smuzhiyun };
747*4882a593Smuzhiyun
at91_twi_configure_dma(struct at91_twi_dev * dev,u32 phy_addr)748*4882a593Smuzhiyun static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
749*4882a593Smuzhiyun {
750*4882a593Smuzhiyun int ret = 0;
751*4882a593Smuzhiyun struct dma_slave_config slave_config;
752*4882a593Smuzhiyun struct at91_twi_dma *dma = &dev->dma;
753*4882a593Smuzhiyun enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun /*
756*4882a593Smuzhiyun * The actual width of the access will be chosen in
757*4882a593Smuzhiyun * dmaengine_prep_slave_sg():
758*4882a593Smuzhiyun * for each buffer in the scatter-gather list, if its size is aligned
759*4882a593Smuzhiyun * to addr_width then addr_width accesses will be performed to transfer
760*4882a593Smuzhiyun * the buffer. On the other hand, if the buffer size is not aligned to
761*4882a593Smuzhiyun * addr_width then the buffer is transferred using single byte accesses.
762*4882a593Smuzhiyun * Please refer to the Atmel eXtended DMA controller driver.
763*4882a593Smuzhiyun * When FIFOs are used, the TXRDYM threshold can always be set to
764*4882a593Smuzhiyun * trigger the XDMAC when at least 4 data can be written into the TX
765*4882a593Smuzhiyun * FIFO, even if single byte accesses are performed.
766*4882a593Smuzhiyun * However the RXRDYM threshold must be set to fit the access width,
767*4882a593Smuzhiyun * deduced from buffer length, so the XDMAC is triggered properly to
768*4882a593Smuzhiyun * read data from the RX FIFO.
769*4882a593Smuzhiyun */
770*4882a593Smuzhiyun if (dev->fifo_size)
771*4882a593Smuzhiyun addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun memset(&slave_config, 0, sizeof(slave_config));
774*4882a593Smuzhiyun slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
775*4882a593Smuzhiyun slave_config.src_addr_width = addr_width;
776*4882a593Smuzhiyun slave_config.src_maxburst = 1;
777*4882a593Smuzhiyun slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
778*4882a593Smuzhiyun slave_config.dst_addr_width = addr_width;
779*4882a593Smuzhiyun slave_config.dst_maxburst = 1;
780*4882a593Smuzhiyun slave_config.device_fc = false;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun dma->chan_tx = dma_request_chan(dev->dev, "tx");
783*4882a593Smuzhiyun if (IS_ERR(dma->chan_tx)) {
784*4882a593Smuzhiyun ret = PTR_ERR(dma->chan_tx);
785*4882a593Smuzhiyun dma->chan_tx = NULL;
786*4882a593Smuzhiyun goto error;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun dma->chan_rx = dma_request_chan(dev->dev, "rx");
790*4882a593Smuzhiyun if (IS_ERR(dma->chan_rx)) {
791*4882a593Smuzhiyun ret = PTR_ERR(dma->chan_rx);
792*4882a593Smuzhiyun dma->chan_rx = NULL;
793*4882a593Smuzhiyun goto error;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun slave_config.direction = DMA_MEM_TO_DEV;
797*4882a593Smuzhiyun if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
798*4882a593Smuzhiyun dev_err(dev->dev, "failed to configure tx channel\n");
799*4882a593Smuzhiyun ret = -EINVAL;
800*4882a593Smuzhiyun goto error;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun slave_config.direction = DMA_DEV_TO_MEM;
804*4882a593Smuzhiyun if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
805*4882a593Smuzhiyun dev_err(dev->dev, "failed to configure rx channel\n");
806*4882a593Smuzhiyun ret = -EINVAL;
807*4882a593Smuzhiyun goto error;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun sg_init_table(dma->sg, 2);
811*4882a593Smuzhiyun dma->buf_mapped = false;
812*4882a593Smuzhiyun dma->xfer_in_progress = false;
813*4882a593Smuzhiyun dev->use_dma = true;
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
816*4882a593Smuzhiyun dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun return ret;
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun error:
821*4882a593Smuzhiyun if (ret != -EPROBE_DEFER)
822*4882a593Smuzhiyun dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n");
823*4882a593Smuzhiyun if (dma->chan_rx)
824*4882a593Smuzhiyun dma_release_channel(dma->chan_rx);
825*4882a593Smuzhiyun if (dma->chan_tx)
826*4882a593Smuzhiyun dma_release_channel(dma->chan_tx);
827*4882a593Smuzhiyun return ret;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
at91_init_twi_recovery_gpio(struct platform_device * pdev,struct at91_twi_dev * dev)830*4882a593Smuzhiyun static int at91_init_twi_recovery_gpio(struct platform_device *pdev,
831*4882a593Smuzhiyun struct at91_twi_dev *dev)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun rinfo->pinctrl = devm_pinctrl_get(&pdev->dev);
836*4882a593Smuzhiyun if (!rinfo->pinctrl || IS_ERR(rinfo->pinctrl)) {
837*4882a593Smuzhiyun dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n");
838*4882a593Smuzhiyun return PTR_ERR(rinfo->pinctrl);
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun dev->adapter.bus_recovery_info = rinfo;
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun return 0;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun
at91_twi_recover_bus_cmd(struct i2c_adapter * adap)845*4882a593Smuzhiyun static int at91_twi_recover_bus_cmd(struct i2c_adapter *adap)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun struct at91_twi_dev *dev = i2c_get_adapdata(adap);
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
850*4882a593Smuzhiyun if (!(dev->transfer_status & AT91_TWI_SDA)) {
851*4882a593Smuzhiyun dev_dbg(dev->dev, "SDA is down; sending bus clear command\n");
852*4882a593Smuzhiyun if (dev->use_alt_cmd) {
853*4882a593Smuzhiyun unsigned int acr;
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun acr = at91_twi_read(dev, AT91_TWI_ACR);
856*4882a593Smuzhiyun acr &= ~AT91_TWI_ACR_DATAL_MASK;
857*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_ACR, acr);
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_CLEAR);
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun return 0;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun
at91_init_twi_recovery_info(struct platform_device * pdev,struct at91_twi_dev * dev)865*4882a593Smuzhiyun static int at91_init_twi_recovery_info(struct platform_device *pdev,
866*4882a593Smuzhiyun struct at91_twi_dev *dev)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
869*4882a593Smuzhiyun bool has_clear_cmd = dev->pdata->has_clear_cmd;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun if (!has_clear_cmd)
872*4882a593Smuzhiyun return at91_init_twi_recovery_gpio(pdev, dev);
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun rinfo->recover_bus = at91_twi_recover_bus_cmd;
875*4882a593Smuzhiyun dev->adapter.bus_recovery_info = rinfo;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun return 0;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
at91_twi_probe_master(struct platform_device * pdev,u32 phy_addr,struct at91_twi_dev * dev)880*4882a593Smuzhiyun int at91_twi_probe_master(struct platform_device *pdev,
881*4882a593Smuzhiyun u32 phy_addr, struct at91_twi_dev *dev)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun int rc;
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun init_completion(&dev->cmd_complete);
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
888*4882a593Smuzhiyun dev_name(dev->dev), dev);
889*4882a593Smuzhiyun if (rc) {
890*4882a593Smuzhiyun dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
891*4882a593Smuzhiyun return rc;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun if (dev->dev->of_node) {
895*4882a593Smuzhiyun rc = at91_twi_configure_dma(dev, phy_addr);
896*4882a593Smuzhiyun if (rc == -EPROBE_DEFER)
897*4882a593Smuzhiyun return rc;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
901*4882a593Smuzhiyun &dev->fifo_size)) {
902*4882a593Smuzhiyun dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun dev->enable_dig_filt = of_property_read_bool(pdev->dev.of_node,
906*4882a593Smuzhiyun "i2c-digital-filter");
907*4882a593Smuzhiyun
908*4882a593Smuzhiyun dev->enable_ana_filt = of_property_read_bool(pdev->dev.of_node,
909*4882a593Smuzhiyun "i2c-analog-filter");
910*4882a593Smuzhiyun at91_calc_twi_clock(dev);
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun rc = at91_init_twi_recovery_info(pdev, dev);
913*4882a593Smuzhiyun if (rc == -EPROBE_DEFER)
914*4882a593Smuzhiyun return rc;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun dev->adapter.algo = &at91_twi_algorithm;
917*4882a593Smuzhiyun dev->adapter.quirks = &at91_twi_quirks;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun return 0;
920*4882a593Smuzhiyun }
921