1 From 97050437c6a3ce59ce2c5a8286b9bc1c9f1b3b60 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Fri, 4 Nov 2011 16:00:34 +0100
4 Subject: [PATCH 40/70] I2C: MIPS: lantiq: add FALC-ON i2c bus master
6 This patch adds the driver needed to make the I2C bus work on FALC-ON SoCs.
8 Signed-off-by: Thomas Langer <thomas.langer@lantiq.com>
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 Cc: linux-i2c@vger.kernel.org
12 .../include/asm/mach-lantiq/falcon/lantiq_soc.h | 5 +
13 arch/mips/lantiq/falcon/clk.c | 44 -
14 arch/mips/lantiq/falcon/devices.c | 16 +
15 arch/mips/lantiq/falcon/devices.h | 1 +
16 arch/mips/lantiq/falcon/mach-easy98000.c | 1 +
17 drivers/i2c/busses/Kconfig | 10 +
18 drivers/i2c/busses/Makefile | 1 +
19 drivers/i2c/busses/i2c-falcon.c | 1040 ++++++++++++++++++++
20 8 files changed, 1074 insertions(+), 44 deletions(-)
21 delete mode 100644 arch/mips/lantiq/falcon/clk.c
22 create mode 100644 drivers/i2c/busses/i2c-falcon.c
24 diff --git a/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h b/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h
25 index 120c56c..fff5ecd 100644
26 --- a/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h
27 +++ b/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h
29 #define LTQ_PADCTRL4_BASE_ADDR 0x1E800600
30 #define LTQ_PADCTRL4_SIZE 0x0100
33 +#define GPON_I2C_BASE 0x1E200000
34 +#define GPON_I2C_SIZE 0x00010000
37 #define LTQ_STATUS_BASE_ADDR 0x1E802000
40 #define ACTS_PADCTRL2 0x00200000
41 #define ACTS_PADCTRL3 0x00200000
42 #define ACTS_PADCTRL4 0x00400000
43 +#define ACTS_I2C_ACT 0x00004000
45 /* global register ranges */
46 extern __iomem void *ltq_ebu_membase;
47 diff --git a/arch/mips/lantiq/falcon/clk.c b/arch/mips/lantiq/falcon/clk.c
48 deleted file mode 100644
49 index afe1b52..0000000
50 --- a/arch/mips/lantiq/falcon/clk.c
54 - * This program is free software; you can redistribute it and/or modify it
55 - * under the terms of the GNU General Public License version 2 as published
56 - * by the Free Software Foundation.
58 - * Copyright (C) 2011 Thomas Langer <thomas.langer@lantiq.com>
59 - * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
62 -#include <linux/ioport.h>
63 -#include <linux/export.h>
65 -#include <lantiq_soc.h>
69 -/* CPU0 Clock Control Register */
70 -#define LTQ_SYS1_CPU0CC 0x0040
71 -/* clock divider bit */
72 -#define LTQ_CPU0CC_CPUDIV 0x0001
75 -ltq_get_io_region_clock(void)
79 -EXPORT_SYMBOL(ltq_get_io_region_clock);
84 - if (ltq_sys1_r32(LTQ_SYS1_CPU0CC) & LTQ_CPU0CC_CPUDIV)
89 -EXPORT_SYMBOL(ltq_get_cpu_hz);
96 -EXPORT_SYMBOL(ltq_get_fpi_hz);
97 diff --git a/arch/mips/lantiq/falcon/devices.c b/arch/mips/lantiq/falcon/devices.c
98 index 92ec571..e684ed4 100644
99 --- a/arch/mips/lantiq/falcon/devices.c
100 +++ b/arch/mips/lantiq/falcon/devices.c
101 @@ -134,3 +134,19 @@ falcon_register_spi_flash(struct spi_board_info *data)
102 spi_register_board_info(data, 1);
103 platform_device_register(<q_spi);
107 +static struct resource falcon_i2c_resources[] = {
108 + MEM_RES("i2c", GPON_I2C_BASE, GPON_I2C_SIZE),
109 + IRQ_RES(i2c_lb, FALCON_IRQ_I2C_LBREQ),
110 + IRQ_RES(i2c_b, FALCON_IRQ_I2C_BREQ),
111 + IRQ_RES(i2c_err, FALCON_IRQ_I2C_I2C_ERR),
112 + IRQ_RES(i2c_p, FALCON_IRQ_I2C_I2C_P),
116 +falcon_register_i2c(void)
118 + platform_device_register_simple("i2c-falcon", 0,
119 + falcon_i2c_resources, ARRAY_SIZE(falcon_i2c_resources));
121 diff --git a/arch/mips/lantiq/falcon/devices.h b/arch/mips/lantiq/falcon/devices.h
122 index 5e6f720..d81edbe 100644
123 --- a/arch/mips/lantiq/falcon/devices.h
124 +++ b/arch/mips/lantiq/falcon/devices.h
125 @@ -20,5 +20,6 @@ extern void falcon_register_nand(void);
126 extern void falcon_register_gpio(void);
127 extern void falcon_register_gpio_extra(void);
128 extern void falcon_register_spi_flash(struct spi_board_info *data);
129 +extern void falcon_register_i2c(void);
132 diff --git a/arch/mips/lantiq/falcon/mach-easy98000.c b/arch/mips/lantiq/falcon/mach-easy98000.c
133 index 1a7caad..fc5720d 100644
134 --- a/arch/mips/lantiq/falcon/mach-easy98000.c
135 +++ b/arch/mips/lantiq/falcon/mach-easy98000.c
136 @@ -98,6 +98,7 @@ easy98000_init_common(void)
138 spi_register_board_info(&easy98000_spi_gpio_devices, 1);
139 platform_device_register(&easy98000_spi_gpio_device);
140 + falcon_register_i2c();
144 diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
145 index a3afac4..41be6cc 100644
146 --- a/drivers/i2c/busses/Kconfig
147 +++ b/drivers/i2c/busses/Kconfig
148 @@ -369,6 +369,16 @@ config I2C_DESIGNWARE_PCI
149 This driver can also be built as a module. If so, the module
150 will be called i2c-designware-pci.
153 + tristate "Falcon I2C interface"
154 + depends on SOC_FALCON
156 + If you say yes to this option, support will be included for the
157 + Lantiq FALC-ON I2C core.
159 + This driver can also be built as a module. If so, the module
160 + will be called i2c-falcon.
163 tristate "GPIO-based bitbanging I2C"
164 depends on GENERIC_GPIO
165 diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
166 index fba6da6..36239c8 100644
167 --- a/drivers/i2c/busses/Makefile
168 +++ b/drivers/i2c/busses/Makefile
169 @@ -37,6 +37,7 @@ obj-$(CONFIG_I2C_DESIGNWARE_PLATFORM) += i2c-designware-platform.o
170 i2c-designware-platform-objs := i2c-designware-platdrv.o i2c-designware-core.o
171 obj-$(CONFIG_I2C_DESIGNWARE_PCI) += i2c-designware-pci.o
172 i2c-designware-pci-objs := i2c-designware-pcidrv.o i2c-designware-core.o
173 +obj-$(CONFIG_I2C_FALCON) += i2c-falcon.o
174 obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o
175 obj-$(CONFIG_I2C_HIGHLANDER) += i2c-highlander.o
176 obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o
177 diff --git a/drivers/i2c/busses/i2c-falcon.c b/drivers/i2c/busses/i2c-falcon.c
179 index 0000000..fc4f0eb
181 +++ b/drivers/i2c/busses/i2c-falcon.c
184 + * Lantiq FALC(tm) ON - I2C bus adapter
186 + * Parts based on i2c-designware.c and other i2c drivers from Linux 2.6.33
188 + * This program is free software; you can redistribute it and/or modify
189 + * it under the terms of the GNU General Public License as published by
190 + * the Free Software Foundation; either version 2 of the License, or
191 + * (at your option) any later version.
193 + * This program is distributed in the hope that it will be useful,
194 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
195 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
196 + * GNU General Public License for more details.
198 + * You should have received a copy of the GNU General Public License
199 + * along with this program; if not, write to the Free Software
200 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
202 + * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
207 + * - no high speed support
208 + * - supports only master mode
209 + * - ten bit mode is not tested (no slave devices)
212 +#include <linux/kernel.h>
213 +#include <linux/module.h>
214 +#include <linux/delay.h>
215 +#include <linux/slab.h>
216 +#include <linux/i2c.h>
217 +#include <linux/clk.h>
218 +#include <linux/errno.h>
219 +#include <linux/sched.h>
220 +#include <linux/err.h>
221 +#include <linux/interrupt.h>
222 +#include <linux/platform_device.h>
223 +#include <linux/io.h>
224 +#include <linux/err.h>
225 +#include <linux/gpio.h>
227 +#include <lantiq_soc.h>
229 +/* I2C Identification Register */
231 +#define I2C_ID_ID_MASK 0x0000FF00
233 +#define I2C_ID_ID_OFFSET 8
235 +#define I2C_ID_REV_MASK 0x000000FF
237 +#define I2C_ID_REV_OFFSET 0
239 +/* I2C Error Interrupt Request Source Status Register */
241 +#define I2C_ERR_IRQSS_TXF_OFL 0x00000008
243 +#define I2C_ERR_IRQSS_TXF_UFL 0x00000004
245 +#define I2C_ERR_IRQSS_RXF_OFL 0x00000002
247 +#define I2C_ERR_IRQSS_RXF_UFL 0x00000001
249 +/* I2C Bus Status Register */
251 +#define I2C_BUS_STAT_BS_MASK 0x00000003
252 +/* I2C Bus is free. */
253 +#define I2C_BUS_STAT_BS_FREE 0x00000000
255 + * The device is working as master and has claimed the control
256 + * on the I2C-bus (busy master).
258 +#define I2C_BUS_STAT_BS_BM 0x00000002
260 +/* I2C Interrupt Clear Register */
262 +#define I2C_ICR_BREQ_INT_CLR 0x00000008
264 +#define I2C_ICR_LBREQ_INT_CLR 0x00000004
266 +/* I2C RUN Control Register */
268 +#define I2C_RUN_CTRL_RUN_EN 0x00000001
270 +/* I2C Kernel Clock Control Register */
272 +#define I2C_CLC_RMC_OFFSET 8
274 +#define I2C_IMSC_I2C_P_INT_EN 0x00000020
276 +#define I2C_IMSC_I2C_ERR_INT_EN 0x00000010
278 +#define I2C_IMSC_BREQ_INT_EN 0x00000008
280 +#define I2C_IMSC_LBREQ_INT_EN 0x00000004
282 +/* I2C Fractional Divider Configuration Register */
284 +#define I2C_FDIV_CFG_INC_OFFSET 16
286 +#define I2C_FDIV_CFG_DEC_OFFSET 0
288 +/* I2C Fractional Divider (highspeed mode) Configuration Register */
290 +#define I2C_FDIV_HIGH_CFG_INC_OFFSET 16
292 +#define I2C_FDIV_HIGH_CFG_DEC_OFFSET 0
294 +/* I2C Address Register */
296 +#define I2C_ADDR_CFG_SOPE_EN 0x00200000
298 +#define I2C_ADDR_CFG_SONA_EN 0x00100000
300 +#define I2C_ADDR_CFG_MnS_EN 0x00080000
302 +/* I2C Protocol Interrupt Request Source Status Register */
304 +#define I2C_P_IRQSS_RX 0x00000040
306 +#define I2C_P_IRQSS_TX_END 0x00000020
308 +#define I2C_P_IRQSS_NACK 0x00000010
310 +#define I2C_P_IRQSS_AL 0x00000008
312 +/* I2C Raw Interrupt Status Register */
313 +/* Read: Interrupt occurred. */
314 +#define I2C_RIS_I2C_P_INT_INTOCC 0x00000020
315 +/* Read: Interrupt occurred. */
316 +#define I2C_RIS_I2C_ERR_INT_INTOCC 0x00000010
318 +/* I2C End Data Control Register */
320 + * Set End of Transmission - Note: Do not write '1' to this bit when bus is
321 + * free. This will cause an abort after the first byte when a new transfer
324 +#define I2C_ENDD_CTRL_SETEND 0x00000002
325 +/* TX FIFO Flow Control */
326 +#define I2C_FIFO_CFG_TXFC 0x00020000
327 +/* RX FIFO Flow Control */
328 +#define I2C_FIFO_CFG_RXFC 0x00010000
329 +/* Word aligned (character alignment of four characters) */
330 +#define I2C_FIFO_CFG_TXFA_TXFA2 0x00002000
331 +/* Word aligned (character alignment of four characters) */
332 +#define I2C_FIFO_CFG_RXFA_RXFA2 0x00000200
334 +#define I2C_FIFO_CFG_TXBS_TXBS0 0x00000000
336 +#define I2C_FIFO_CFG_RXBS_RXBS0 0x00000000
339 +/* I2C register structure */
340 +struct gpon_reg_i2c {
341 + /* I2C Kernel Clock Control Register */
342 + unsigned int clc; /* 0x00000000 */
344 + unsigned int res_0; /* 0x00000004 */
345 + /* I2C Identification Register */
346 + unsigned int id; /* 0x00000008 */
348 + unsigned int res_1; /* 0x0000000C */
350 + * I2C RUN Control Register - This register enables and disables the I2C
351 + * peripheral. Before enabling, the I2C has to be configured properly.
352 + * After enabling no configuration is possible
354 + unsigned int run_ctrl; /* 0x00000010 */
356 + * I2C End Data Control Register - This register is used to either turn
357 + * around the data transmission direction or to address another slave
358 + * without sending a stop condition. Also the software can stop the
359 + * slave-transmitter by sending a not-accolade when working as
360 + * master-receiver or even stop data transmission immediately when
361 + * operating as master-transmitter. The writing to the bits of this
362 + * control register is only effective when in MASTER RECEIVES BYTES,
363 + * MASTER TRANSMITS BYTES, MASTER RESTART or SLAVE RECEIVE BYTES state
365 + unsigned int endd_ctrl; /* 0x00000014 */
367 + * I2C Fractional Divider Configuration Register - These register is
368 + * used to program the fractional divider of the I2C bus. Before the
369 + * peripheral is switched on by setting the RUN-bit the two (fixed)
370 + * values for the two operating frequencies are programmed into these
371 + * (configuration) registers. The Register FDIV_HIGH_CFG has the same
372 + * layout as I2C_FDIV_CFG.
374 + unsigned int fdiv_cfg; /* 0x00000018 */
376 + * I2C Fractional Divider (highspeed mode) Configuration Register
377 + * These register is used to program the fractional divider of the I2C
378 + * bus. Before the peripheral is switched on by setting the RUN-bit the
379 + * two (fixed) values for the two operating frequencies are programmed
380 + * into these (configuration) registers. The Register FDIV_CFG has the
381 + * same layout as I2C_FDIV_CFG.
383 + unsigned int fdiv_high_cfg; /* 0x0000001C */
384 + /* I2C Address Configuration Register */
385 + unsigned int addr_cfg; /* 0x00000020 */
387 + * I2C Bus Status Register - This register gives a status information
388 + * of the I2C. This additional information can be used by the software
389 + * to start proper actions.
391 + unsigned int bus_stat; /* 0x00000024 */
392 + /* I2C FIFO Configuration Register */
393 + unsigned int fifo_cfg; /* 0x00000028 */
394 + /* I2C Maximum Received Packet Size Register */
395 + unsigned int mrps_ctrl; /* 0x0000002C */
396 + /* I2C Received Packet Size Status Register */
397 + unsigned int rps_stat; /* 0x00000030 */
398 + /* I2C Transmit Packet Size Register */
399 + unsigned int tps_ctrl; /* 0x00000034 */
400 + /* I2C Filled FIFO Stages Status Register */
401 + unsigned int ffs_stat; /* 0x00000038 */
403 + unsigned int res_2; /* 0x0000003C */
404 + /* I2C Timing Configuration Register */
405 + unsigned int tim_cfg; /* 0x00000040 */
407 + unsigned int res_3[7]; /* 0x00000044 */
408 + /* I2C Error Interrupt Request Source Mask Register */
409 + unsigned int err_irqsm; /* 0x00000060 */
410 + /* I2C Error Interrupt Request Source Status Register */
411 + unsigned int err_irqss; /* 0x00000064 */
412 + /* I2C Error Interrupt Request Source Clear Register */
413 + unsigned int err_irqsc; /* 0x00000068 */
415 + unsigned int res_4; /* 0x0000006C */
416 + /* I2C Protocol Interrupt Request Source Mask Register */
417 + unsigned int p_irqsm; /* 0x00000070 */
418 + /* I2C Protocol Interrupt Request Source Status Register */
419 + unsigned int p_irqss; /* 0x00000074 */
420 + /* I2C Protocol Interrupt Request Source Clear Register */
421 + unsigned int p_irqsc; /* 0x00000078 */
423 + unsigned int res_5; /* 0x0000007C */
424 + /* I2C Raw Interrupt Status Register */
425 + unsigned int ris; /* 0x00000080 */
426 + /* I2C Interrupt Mask Control Register */
427 + unsigned int imsc; /* 0x00000084 */
428 + /* I2C Masked Interrupt Status Register */
429 + unsigned int mis; /* 0x00000088 */
430 + /* I2C Interrupt Clear Register */
431 + unsigned int icr; /* 0x0000008C */
432 + /* I2C Interrupt Set Register */
433 + unsigned int isr; /* 0x00000090 */
434 + /* I2C DMA Enable Register */
435 + unsigned int dmae; /* 0x00000094 */
437 + unsigned int res_6[8154]; /* 0x00000098 */
438 + /* I2C Transmit Data Register */
439 + unsigned int txd; /* 0x00008000 */
441 + unsigned int res_7[4095]; /* 0x00008004 */
442 + /* I2C Receive Data Register */
443 + unsigned int rxd; /* 0x0000C000 */
445 + unsigned int res_8[4095]; /* 0x0000C004 */
448 +/* mapping for access macros */
449 +#define i2c ((struct gpon_reg_i2c *)priv->membase)
450 +#define reg_r32(reg) __raw_readl(reg)
451 +#define reg_w32(val, reg) __raw_writel(val, reg)
452 +#define reg_w32_mask(clear, set, reg) \
453 + reg_w32((reg_r32(reg) & ~(clear)) | (set), reg)
454 +#define reg_r32_table(reg, idx) reg_r32(&((uint32_t *)®)[idx])
455 +#define reg_w32_table(val, reg, idx) reg_w32(val, &((uint32_t *)®)[idx])
457 +#define i2c_r32(reg) reg_r32(&i2c->reg)
458 +#define i2c_w32(val, reg) reg_w32(val, &i2c->reg)
459 +#define i2c_w32_mask(clear, set, reg) reg_w32_mask(clear, set, &i2c->reg)
461 +#define DRV_NAME "i2c-falcon"
462 +#define DRV_VERSION "1.01"
464 +#define FALCON_I2C_BUSY_TIMEOUT 20 /* ms */
467 +#define FALCON_I2C_XFER_TIMEOUT (25 * HZ)
469 +#define FALCON_I2C_XFER_TIMEOUT HZ
471 +#if defined(DEBUG) && 0
472 +#define PRINTK(arg...) pr_info(arg)
474 +#define PRINTK(arg...) do {} while (0)
477 +#define FALCON_I2C_IMSC_DEFAULT_MASK (I2C_IMSC_I2C_P_INT_EN | \
478 + I2C_IMSC_I2C_ERR_INT_EN)
480 +#define FALCON_I2C_ARB_LOST (1 << 0)
481 +#define FALCON_I2C_NACK (1 << 1)
482 +#define FALCON_I2C_RX_UFL (1 << 2)
483 +#define FALCON_I2C_RX_OFL (1 << 3)
484 +#define FALCON_I2C_TX_UFL (1 << 4)
485 +#define FALCON_I2C_TX_OFL (1 << 5)
488 + struct mutex mutex;
491 + FALCON_I2C_MODE_100 = 1,
492 + FALCON_I2C_MODE_400 = 2,
493 + FALCON_I2C_MODE_3400 = 3
494 + } mode; /* current speed mode */
496 + struct clk *clk; /* clock input for i2c hardware block */
497 + struct gpon_reg_i2c __iomem *membase; /* base of mapped registers */
498 + int irq_lb, irq_b, irq_err, irq_p; /* last burst, burst, error,
501 + struct i2c_adapter adap;
502 + struct device *dev;
504 + struct completion cmd_complete;
506 + /* message transfer data */
507 + /* current message */
508 + struct i2c_msg *current_msg;
509 + /* number of messages to handle */
511 + /* current buffer */
513 + /* remaining length of current buffer */
515 + /* error status of the current transfer */
518 + /* master status codes */
521 + STATUS_ADDR, /* address phase */
529 +static irqreturn_t falcon_i2c_isr(int irq, void *dev_id);
531 +static inline void enable_burst_irq(struct falcon_i2c *priv)
533 + i2c_w32_mask(0, I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, imsc);
535 +static inline void disable_burst_irq(struct falcon_i2c *priv)
537 + i2c_w32_mask(I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, 0, imsc);
540 +static void prepare_msg_send_addr(struct falcon_i2c *priv)
542 + struct i2c_msg *msg = priv->current_msg;
543 + int rd = !!(msg->flags & I2C_M_RD);
544 + u16 addr = msg->addr;
547 + priv->msg_buf = msg->buf;
548 + priv->msg_buf_len = msg->len;
550 + priv->status = STATUS_READ;
552 + priv->status = STATUS_WRITE;
554 + /* send slave address */
555 + if (msg->flags & I2C_M_TEN) {
556 + i2c_w32(0xf0 | ((addr & 0x300) >> 7) | rd, txd);
557 + i2c_w32(addr & 0xff, txd);
559 + i2c_w32((addr & 0x7f) << 1 | rd, txd);
562 +static void set_tx_len(struct falcon_i2c *priv)
564 + struct i2c_msg *msg = priv->current_msg;
565 + int len = (msg->flags & I2C_M_TEN) ? 2 : 1;
567 + PRINTK("set_tx_len %cX\n", (msg->flags & I2C_M_RD) ? ('R') : ('T'));
569 + priv->status = STATUS_ADDR;
571 + if (!(msg->flags & I2C_M_RD)) {
574 + /* set maximum received packet size (before rx int!) */
575 + i2c_w32(msg->len, mrps_ctrl);
577 + i2c_w32(len, tps_ctrl);
578 + enable_burst_irq(priv);
581 +static int falcon_i2c_hw_init(struct i2c_adapter *adap)
583 + struct falcon_i2c *priv = i2c_get_adapdata(adap);
586 + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
589 + /* set normal operation clock divider */
590 + i2c_w32(1 << I2C_CLC_RMC_OFFSET, clc);
592 + /* for debugging a higher divider value! */
593 + i2c_w32(0xF0 << I2C_CLC_RMC_OFFSET, clc);
596 + /* set frequency */
597 + if (priv->mode == FALCON_I2C_MODE_100) {
598 + dev_dbg(priv->dev, "set standard mode (100 kHz)\n");
599 + i2c_w32(0, fdiv_high_cfg);
600 + i2c_w32((1 << I2C_FDIV_CFG_INC_OFFSET) |
601 + (499 << I2C_FDIV_CFG_DEC_OFFSET),
603 + } else if (priv->mode == FALCON_I2C_MODE_400) {
604 + dev_dbg(priv->dev, "set fast mode (400 kHz)\n");
605 + i2c_w32(0, fdiv_high_cfg);
606 + i2c_w32((1 << I2C_FDIV_CFG_INC_OFFSET) |
607 + (124 << I2C_FDIV_CFG_DEC_OFFSET),
609 + } else if (priv->mode == FALCON_I2C_MODE_3400) {
610 + dev_dbg(priv->dev, "set high mode (3.4 MHz)\n");
611 + i2c_w32(0, fdiv_cfg);
612 + /* TODO recalculate value for 100MHz input */
613 + i2c_w32((41 << I2C_FDIV_HIGH_CFG_INC_OFFSET) |
614 + (152 << I2C_FDIV_HIGH_CFG_DEC_OFFSET),
617 + dev_warn(priv->dev, "unknown mode\n");
621 + /* configure fifo */
622 + i2c_w32(I2C_FIFO_CFG_TXFC | /* tx fifo as flow controller */
623 + I2C_FIFO_CFG_RXFC | /* rx fifo as flow controller */
624 + I2C_FIFO_CFG_TXFA_TXFA2 | /* tx fifo 4-byte aligned */
625 + I2C_FIFO_CFG_RXFA_RXFA2 | /* rx fifo 4-byte aligned */
626 + I2C_FIFO_CFG_TXBS_TXBS0 | /* tx fifo burst size is 1 word */
627 + I2C_FIFO_CFG_RXBS_RXBS0, /* rx fifo burst size is 1 word */
630 + /* configure address */
631 + i2c_w32(I2C_ADDR_CFG_SOPE_EN | /* generate stop when no more data
633 + I2C_ADDR_CFG_SONA_EN | /* generate stop when NA received */
634 + I2C_ADDR_CFG_MnS_EN | /* we are master device */
635 + 0, /* our slave address (not used!) */
639 + i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
644 +static int falcon_i2c_wait_bus_not_busy(struct falcon_i2c *priv)
646 + int timeout = FALCON_I2C_BUSY_TIMEOUT;
648 + while ((i2c_r32(bus_stat) & I2C_BUS_STAT_BS_MASK)
649 + != I2C_BUS_STAT_BS_FREE) {
650 + if (timeout <= 0) {
651 + dev_warn(priv->dev, "timeout waiting for bus ready\n");
661 +static void falcon_i2c_tx(struct falcon_i2c *priv, int last)
663 + if (priv->msg_buf_len && priv->msg_buf) {
664 + i2c_w32(*priv->msg_buf, txd);
666 + if (--priv->msg_buf_len)
669 + priv->msg_buf = NULL;
674 + disable_burst_irq(priv);
677 +static void falcon_i2c_rx(struct falcon_i2c *priv, int last)
679 + u32 fifo_stat, timeout;
680 + if (priv->msg_buf_len && priv->msg_buf) {
683 + fifo_stat = i2c_r32(ffs_stat);
684 + } while (!fifo_stat && --timeout);
687 + PRINTK("\nrx timeout\n");
690 + while (fifo_stat) {
691 + *priv->msg_buf = i2c_r32(rxd);
692 + if (--priv->msg_buf_len)
695 + priv->msg_buf = NULL;
700 + fifo_stat = i2c_r32(ffs_stat);
702 + /* do not read more than burst size, otherwise no "last
703 + burst" is generated and the transaction is blocked! */
712 + disable_burst_irq(priv);
714 + if (priv->status == STATUS_READ_END) {
715 + /* do the STATUS_STOP and complete() here, as sometimes
716 + the tx_end is already seen before this is finished */
717 + priv->status = STATUS_STOP;
718 + complete(&priv->cmd_complete);
720 + i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
721 + priv->status = STATUS_READ_END;
726 +static void falcon_i2c_xfer_init(struct falcon_i2c *priv)
728 + /* enable interrupts */
729 + i2c_w32(FALCON_I2C_IMSC_DEFAULT_MASK, imsc);
731 + /* trigger transfer of first msg */
735 +static void dump_msgs(struct i2c_msg msgs[], int num, int rx)
739 + pr_info("Messages %d %s\n", num, rx ? "out" : "in");
740 + for (i = 0; i < num; i++) {
741 + pr_info("%2d %cX Msg(%d) addr=0x%X: ", i,
742 + (msgs[i].flags & I2C_M_RD) ? ('R') : ('T'),
743 + msgs[i].len, msgs[i].addr);
744 + if (!(msgs[i].flags & I2C_M_RD) || rx) {
745 + for (j = 0; j < msgs[i].len; j++)
746 + printk("%02X ", msgs[i].buf[j]);
753 +static void falcon_i2c_release_bus(struct falcon_i2c *priv)
755 + if ((i2c_r32(bus_stat) & I2C_BUS_STAT_BS_MASK) == I2C_BUS_STAT_BS_BM)
756 + i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
759 +static int falcon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
762 + struct falcon_i2c *priv = i2c_get_adapdata(adap);
765 + dev_dbg(priv->dev, "xfer %u messages\n", num);
766 + dump_msgs(msgs, num, 0);
768 + mutex_lock(&priv->mutex);
770 + INIT_COMPLETION(priv->cmd_complete);
771 + priv->current_msg = msgs;
772 + priv->msgs_num = num;
774 + priv->status = STATUS_IDLE;
776 + /* wait for the bus to become ready */
777 + ret = falcon_i2c_wait_bus_not_busy(priv);
781 + while (priv->msgs_num) {
782 + /* start the transfers */
783 + falcon_i2c_xfer_init(priv);
785 + /* wait for transfers to complete */
786 + ret = wait_for_completion_interruptible_timeout(
787 + &priv->cmd_complete, FALCON_I2C_XFER_TIMEOUT);
789 + dev_err(priv->dev, "controller timed out\n");
790 + falcon_i2c_hw_init(adap);
793 + } else if (ret < 0)
796 + if (priv->msg_err) {
797 + if (priv->msg_err & FALCON_I2C_NACK)
803 + if (--priv->msgs_num)
804 + priv->current_msg++;
810 + falcon_i2c_release_bus(priv);
812 + mutex_unlock(&priv->mutex);
815 + dump_msgs(msgs, num, 1);
817 + PRINTK("XFER ret %d\n", ret);
821 +static irqreturn_t falcon_i2c_isr_burst(int irq, void *dev_id)
823 + struct falcon_i2c *priv = dev_id;
824 + struct i2c_msg *msg = priv->current_msg;
825 + int last = (irq == priv->irq_lb);
832 + if (msg->flags & I2C_M_RD) {
833 + switch (priv->status) {
836 + prepare_msg_send_addr(priv);
837 + disable_burst_irq(priv);
840 + case STATUS_READ_END:
842 + falcon_i2c_rx(priv, last);
845 + disable_burst_irq(priv);
846 + PRINTK("Status R %d\n", priv->status);
850 + switch (priv->status) {
853 + prepare_msg_send_addr(priv);
857 + falcon_i2c_tx(priv, last);
860 + disable_burst_irq(priv);
861 + PRINTK("Status W %d\n", priv->status);
866 + i2c_w32(I2C_ICR_BREQ_INT_CLR | I2C_ICR_LBREQ_INT_CLR, icr);
867 + return IRQ_HANDLED;
870 +static void falcon_i2c_isr_prot(struct falcon_i2c *priv)
872 + u32 i_pro = i2c_r32(p_irqss);
876 + /* not acknowledge */
877 + if (i_pro & I2C_P_IRQSS_NACK) {
878 + priv->msg_err |= FALCON_I2C_NACK;
882 + /* arbitration lost */
883 + if (i_pro & I2C_P_IRQSS_AL) {
884 + priv->msg_err |= FALCON_I2C_ARB_LOST;
885 + PRINTK(" arb-lost");
887 + /* tx -> rx switch */
888 + if (i_pro & I2C_P_IRQSS_RX)
892 + if (i_pro & I2C_P_IRQSS_TX_END)
896 + if (!priv->msg_err) {
897 + /* tx -> rx switch */
898 + if (i_pro & I2C_P_IRQSS_RX) {
899 + priv->status = STATUS_READ;
900 + enable_burst_irq(priv);
902 + if (i_pro & I2C_P_IRQSS_TX_END) {
903 + if (priv->status == STATUS_READ)
904 + priv->status = STATUS_READ_END;
906 + disable_burst_irq(priv);
907 + priv->status = STATUS_STOP;
912 + i2c_w32(i_pro, p_irqsc);
915 +static irqreturn_t falcon_i2c_isr(int irq, void *dev_id)
917 + u32 i_raw, i_err = 0;
918 + struct falcon_i2c *priv = dev_id;
920 + i_raw = i2c_r32(mis);
921 + PRINTK("i_raw 0x%08X\n", i_raw);
923 + /* error interrupt */
924 + if (i_raw & I2C_RIS_I2C_ERR_INT_INTOCC) {
925 + i_err = i2c_r32(err_irqss);
926 + PRINTK("i_err 0x%08X bus_stat 0x%04X\n",
927 + i_err, i2c_r32(bus_stat));
929 + /* tx fifo overflow (8) */
930 + if (i_err & I2C_ERR_IRQSS_TXF_OFL)
931 + priv->msg_err |= FALCON_I2C_TX_OFL;
933 + /* tx fifo underflow (4) */
934 + if (i_err & I2C_ERR_IRQSS_TXF_UFL)
935 + priv->msg_err |= FALCON_I2C_TX_UFL;
937 + /* rx fifo overflow (2) */
938 + if (i_err & I2C_ERR_IRQSS_RXF_OFL)
939 + priv->msg_err |= FALCON_I2C_RX_OFL;
941 + /* rx fifo underflow (1) */
942 + if (i_err & I2C_ERR_IRQSS_RXF_UFL)
943 + priv->msg_err |= FALCON_I2C_RX_UFL;
945 + i2c_w32(i_err, err_irqsc);
948 + /* protocol interrupt */
949 + if (i_raw & I2C_RIS_I2C_P_INT_INTOCC)
950 + falcon_i2c_isr_prot(priv);
952 + if ((priv->msg_err) || (priv->status == STATUS_STOP))
953 + complete(&priv->cmd_complete);
955 + return IRQ_HANDLED;
958 +static u32 falcon_i2c_functionality(struct i2c_adapter *adap)
960 + return I2C_FUNC_I2C |
961 + I2C_FUNC_10BIT_ADDR |
962 + I2C_FUNC_SMBUS_EMUL;
965 +static struct i2c_algorithm falcon_i2c_algorithm = {
966 + .master_xfer = falcon_i2c_xfer,
967 + .functionality = falcon_i2c_functionality,
970 +static int __devinit falcon_i2c_probe(struct platform_device *pdev)
973 + struct falcon_i2c *priv;
974 + struct i2c_adapter *adap;
975 + struct resource *mmres, *ioarea,
976 + *irqres_lb, *irqres_b, *irqres_err, *irqres_p;
979 + dev_dbg(&pdev->dev, "probing\n");
981 + mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
982 + irqres_lb = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
984 + irqres_b = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "i2c_b");
985 + irqres_err = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
987 + irqres_p = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "i2c_p");
989 + if (!mmres || !irqres_lb || !irqres_b || !irqres_err || !irqres_p) {
990 + dev_err(&pdev->dev, "no resources\n");
994 + clk = clk_get_fpi();
996 + dev_err(&pdev->dev, "failed to get fpi clk\n");
1000 + if (clk_get_rate(clk) != 100000000) {
1001 + dev_err(&pdev->dev, "input clock is not 100MHz\n");
1004 + clk = clk_get(&pdev->dev, NULL);
1005 + if (IS_ERR(clk)) {
1006 + dev_err(&pdev->dev, "failed to get i2c clk\n");
1010 + /* allocate private data */
1011 + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1013 + dev_err(&pdev->dev, "can't allocate private data\n");
1017 + adap = &priv->adap;
1018 + i2c_set_adapdata(adap, priv);
1019 + adap->owner = THIS_MODULE;
1020 + adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1021 + strlcpy(adap->name, DRV_NAME "-adapter", sizeof(adap->name));
1022 + adap->algo = &falcon_i2c_algorithm;
1024 + priv->mode = FALCON_I2C_MODE_100;
1026 + priv->dev = &pdev->dev;
1028 + init_completion(&priv->cmd_complete);
1029 + mutex_init(&priv->mutex);
1031 + if (ltq_gpio_request(&pdev->dev, 107, 0, 0, DRV_NAME":sda") ||
1032 + ltq_gpio_request(&pdev->dev, 108, 0, 0, DRV_NAME":scl"))
1034 + dev_err(&pdev->dev, "I2C gpios not available\n");
1036 + goto err_free_priv;
1039 + ioarea = request_mem_region(mmres->start, resource_size(mmres),
1042 + if (ioarea == NULL) {
1043 + dev_err(&pdev->dev, "I2C region already claimed\n");
1045 + goto err_free_gpio;
1049 + priv->membase = ioremap_nocache(mmres->start & ~KSEG1,
1050 + resource_size(mmres));
1051 + if (priv->membase == NULL) {
1053 + goto err_release_region;
1056 + priv->irq_lb = irqres_lb->start;
1057 + ret = request_irq(priv->irq_lb, falcon_i2c_isr_burst, IRQF_DISABLED,
1058 + irqres_lb->name, priv);
1060 + dev_err(&pdev->dev, "can't get last burst IRQ %d\n",
1061 + irqres_lb->start);
1063 + goto err_unmap_mem;
1066 + priv->irq_b = irqres_b->start;
1067 + ret = request_irq(priv->irq_b, falcon_i2c_isr_burst, IRQF_DISABLED,
1068 + irqres_b->name, priv);
1070 + dev_err(&pdev->dev, "can't get burst IRQ %d\n",
1073 + goto err_free_lb_irq;
1076 + priv->irq_err = irqres_err->start;
1077 + ret = request_irq(priv->irq_err, falcon_i2c_isr, IRQF_DISABLED,
1078 + irqres_err->name, priv);
1080 + dev_err(&pdev->dev, "can't get error IRQ %d\n",
1081 + irqres_err->start);
1083 + goto err_free_b_irq;
1086 + priv->irq_p = irqres_p->start;
1087 + ret = request_irq(priv->irq_p, falcon_i2c_isr, IRQF_DISABLED,
1088 + irqres_p->name, priv);
1090 + dev_err(&pdev->dev, "can't get protocol IRQ %d\n",
1093 + goto err_free_err_irq;
1096 + dev_dbg(&pdev->dev, "mapped io-space to %p\n", priv->membase);
1097 + dev_dbg(&pdev->dev, "use IRQs %d, %d, %d, %d\n", irqres_lb->start,
1098 + irqres_b->start, irqres_err->start, irqres_p->start);
1100 + /* add our adapter to the i2c stack */
1101 + ret = i2c_add_numbered_adapter(adap);
1103 + dev_err(&pdev->dev, "can't register I2C adapter\n");
1104 + goto err_free_p_irq;
1107 + platform_set_drvdata(pdev, priv);
1108 + i2c_set_adapdata(adap, priv);
1110 + /* print module version information */
1111 + dev_dbg(&pdev->dev, "module id=%u revision=%u\n",
1112 + (i2c_r32(id) & I2C_ID_ID_MASK) >> I2C_ID_ID_OFFSET,
1113 + (i2c_r32(id) & I2C_ID_REV_MASK) >> I2C_ID_REV_OFFSET);
1115 + /* initialize HW */
1116 + ret = falcon_i2c_hw_init(adap);
1118 + dev_err(&pdev->dev, "can't configure adapter\n");
1119 + goto err_remove_adapter;
1122 + dev_info(&pdev->dev, "version %s\n", DRV_VERSION);
1126 +err_remove_adapter:
1127 + i2c_del_adapter(adap);
1128 + platform_set_drvdata(pdev, NULL);
1131 + free_irq(priv->irq_p, priv);
1134 + free_irq(priv->irq_err, priv);
1137 + free_irq(priv->irq_b, priv);
1140 + free_irq(priv->irq_lb, priv);
1143 + iounmap(priv->membase);
1145 +err_release_region:
1146 + release_mem_region(mmres->start, resource_size(mmres));
1158 +static int __devexit falcon_i2c_remove(struct platform_device *pdev)
1160 + struct falcon_i2c *priv = platform_get_drvdata(pdev);
1161 + struct resource *mmres;
1164 + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
1166 + /* remove driver */
1167 + platform_set_drvdata(pdev, NULL);
1168 + i2c_del_adapter(&priv->adap);
1170 + free_irq(priv->irq_lb, priv);
1171 + free_irq(priv->irq_b, priv);
1172 + free_irq(priv->irq_err, priv);
1173 + free_irq(priv->irq_p, priv);
1175 + iounmap(priv->membase);
1182 + mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 + release_mem_region(mmres->start, resource_size(mmres));
1185 + dev_dbg(&pdev->dev, "removed\n");
1190 +static struct platform_driver falcon_i2c_driver = {
1191 + .probe = falcon_i2c_probe,
1192 + .remove = __devexit_p(falcon_i2c_remove),
1195 + .owner = THIS_MODULE,
1199 +static int __init falcon_i2c_init(void)
1203 + ret = platform_driver_register(&falcon_i2c_driver);
1206 + pr_debug(DRV_NAME ": can't register platform driver\n");
1211 +static void __exit falcon_i2c_exit(void)
1213 + platform_driver_unregister(&falcon_i2c_driver);
1216 +module_init(falcon_i2c_init);
1217 +module_exit(falcon_i2c_exit);
1219 +MODULE_DESCRIPTION("Lantiq FALC(tm) ON - I2C bus adapter");
1220 +MODULE_ALIAS("platform:" DRV_NAME);
1221 +MODULE_LICENSE("GPL");
1222 +MODULE_VERSION(DRV_VERSION);