0031-I2C-MIPS-lantiq-add-FALC-ON-i2c-bus-master.patch 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. From f17e50f67fa3c77624edf2ca03fae0d50f0ce39b Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Thu, 7 Aug 2014 18:26:42 +0200
  4. Subject: [PATCH 31/36] I2C: MIPS: lantiq: add FALC-ON i2c bus master
  5. This patch adds the driver needed to make the I2C bus work on FALC-ON SoCs.
  6. Signed-off-by: Thomas Langer <[email protected]>
  7. Signed-off-by: John Crispin <[email protected]>
  8. ---
  9. drivers/i2c/busses/Kconfig | 10 +
  10. drivers/i2c/busses/Makefile | 1 +
  11. drivers/i2c/busses/i2c-lantiq.c | 747 +++++++++++++++++++++++++++++++++++++++
  12. drivers/i2c/busses/i2c-lantiq.h | 234 ++++++++++++
  13. 4 files changed, 992 insertions(+)
  14. create mode 100644 drivers/i2c/busses/i2c-lantiq.c
  15. create mode 100644 drivers/i2c/busses/i2c-lantiq.h
  16. --- a/drivers/i2c/busses/Kconfig
  17. +++ b/drivers/i2c/busses/Kconfig
  18. @@ -819,6 +819,16 @@ config I2C_MICROCHIP_CORE
  19. This driver can also be built as a module. If so, the module will be
  20. called i2c-microchip-core.
  21. +config I2C_LANTIQ
  22. + tristate "Lantiq I2C interface"
  23. + depends on LANTIQ && SOC_FALCON
  24. + help
  25. + If you say yes to this option, support will be included for the
  26. + Lantiq I2C core.
  27. +
  28. + This driver can also be built as a module. If so, the module
  29. + will be called i2c-lantiq.
  30. +
  31. config I2C_MPC
  32. tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
  33. depends on PPC
  34. --- a/drivers/i2c/busses/Makefile
  35. +++ b/drivers/i2c/busses/Makefile
  36. @@ -76,6 +76,7 @@ obj-$(CONFIG_I2C_IMX_LPI2C) += i2c-imx-l
  37. obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o
  38. obj-$(CONFIG_I2C_JZ4780) += i2c-jz4780.o
  39. obj-$(CONFIG_I2C_KEMPLD) += i2c-kempld.o
  40. +obj-$(CONFIG_I2C_LANTIQ) += i2c-lantiq.o
  41. obj-$(CONFIG_I2C_LPC2K) += i2c-lpc2k.o
  42. obj-$(CONFIG_I2C_LS2X) += i2c-ls2x.o
  43. obj-$(CONFIG_I2C_MESON) += i2c-meson.o
  44. --- /dev/null
  45. +++ b/drivers/i2c/busses/i2c-lantiq.c
  46. @@ -0,0 +1,742 @@
  47. +
  48. +/*
  49. + * Lantiq I2C bus adapter
  50. + *
  51. + * Parts based on i2c-designware.c and other i2c drivers from Linux 2.6.33
  52. + *
  53. + * This program is free software; you can redistribute it and/or modify
  54. + * it under the terms of the GNU General Public License as published by
  55. + * the Free Software Foundation; either version 2 of the License, or
  56. + * (at your option) any later version.
  57. + *
  58. + * This program is distributed in the hope that it will be useful,
  59. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  60. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  61. + * GNU General Public License for more details.
  62. + *
  63. + * You should have received a copy of the GNU General Public License
  64. + * along with this program; if not, write to the Free Software
  65. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  66. + *
  67. + * Copyright (C) 2012 Thomas Langer <[email protected]>
  68. + */
  69. +
  70. +#include <linux/kernel.h>
  71. +#include <linux/module.h>
  72. +#include <linux/delay.h>
  73. +#include <linux/slab.h> /* for kzalloc, kfree */
  74. +#include <linux/i2c.h>
  75. +#include <linux/errno.h>
  76. +#include <linux/completion.h>
  77. +#include <linux/interrupt.h>
  78. +#include <linux/platform_device.h>
  79. +#include <linux/io.h>
  80. +#include <linux/of_irq.h>
  81. +
  82. +#include <lantiq_soc.h>
  83. +#include "i2c-lantiq.h"
  84. +
  85. +/*
  86. + * CURRENT ISSUES:
  87. + * - no high speed support
  88. + * - ten bit mode is not tested (no slave devices)
  89. + */
  90. +
  91. +/* access macros */
  92. +#define i2c_r32(reg) \
  93. + __raw_readl(&(priv->membase)->reg)
  94. +#define i2c_w32(val, reg) \
  95. + __raw_writel(val, &(priv->membase)->reg)
  96. +#define i2c_w32_mask(clear, set, reg) \
  97. + i2c_w32((i2c_r32(reg) & ~(clear)) | (set), reg)
  98. +
  99. +#define DRV_NAME "i2c-lantiq"
  100. +#define DRV_VERSION "1.00"
  101. +
  102. +#define LTQ_I2C_BUSY_TIMEOUT 20 /* ms */
  103. +
  104. +#ifdef DEBUG
  105. +#define LTQ_I2C_XFER_TIMEOUT (25*HZ)
  106. +#else
  107. +#define LTQ_I2C_XFER_TIMEOUT HZ
  108. +#endif
  109. +
  110. +#define LTQ_I2C_IMSC_DEFAULT_MASK (I2C_IMSC_I2C_P_INT_EN | \
  111. + I2C_IMSC_I2C_ERR_INT_EN)
  112. +
  113. +#define LTQ_I2C_ARB_LOST (1 << 0)
  114. +#define LTQ_I2C_NACK (1 << 1)
  115. +#define LTQ_I2C_RX_UFL (1 << 2)
  116. +#define LTQ_I2C_RX_OFL (1 << 3)
  117. +#define LTQ_I2C_TX_UFL (1 << 4)
  118. +#define LTQ_I2C_TX_OFL (1 << 5)
  119. +
  120. +struct ltq_i2c {
  121. + struct mutex mutex;
  122. +
  123. +
  124. + /* active clock settings */
  125. + unsigned int input_clock; /* clock input for i2c hardware block */
  126. + unsigned int i2c_clock; /* approximated bus clock in kHz */
  127. +
  128. + struct clk *clk_gate;
  129. + struct clk *clk_input;
  130. +
  131. +
  132. + /* resources (memory and interrupts) */
  133. + int irq_lb; /* last burst irq */
  134. +
  135. + struct lantiq_reg_i2c __iomem *membase; /* base of mapped registers */
  136. +
  137. + struct i2c_adapter adap;
  138. + struct device *dev;
  139. +
  140. + struct completion cmd_complete;
  141. +
  142. +
  143. + /* message transfer data */
  144. + struct i2c_msg *current_msg; /* current message */
  145. + int msgs_num; /* number of messages to handle */
  146. + u8 *msg_buf; /* current buffer */
  147. + u32 msg_buf_len; /* remaining length of current buffer */
  148. + int msg_err; /* error status of the current transfer */
  149. +
  150. +
  151. + /* master status codes */
  152. + enum {
  153. + STATUS_IDLE,
  154. + STATUS_ADDR, /* address phase */
  155. + STATUS_WRITE,
  156. + STATUS_READ,
  157. + STATUS_READ_END,
  158. + STATUS_STOP
  159. + } status;
  160. +};
  161. +
  162. +static irqreturn_t ltq_i2c_isr(int irq, void *dev_id);
  163. +
  164. +static inline void enable_burst_irq(struct ltq_i2c *priv)
  165. +{
  166. + i2c_w32_mask(0, I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, imsc);
  167. +}
  168. +static inline void disable_burst_irq(struct ltq_i2c *priv)
  169. +{
  170. + i2c_w32_mask(I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, 0, imsc);
  171. +}
  172. +
  173. +static void prepare_msg_send_addr(struct ltq_i2c *priv)
  174. +{
  175. + struct i2c_msg *msg = priv->current_msg;
  176. + int rd = !!(msg->flags & I2C_M_RD); /* extends to 0 or 1 */
  177. + u16 addr = msg->addr;
  178. +
  179. + /* new i2c_msg */
  180. + priv->msg_buf = msg->buf;
  181. + priv->msg_buf_len = msg->len;
  182. + if (rd)
  183. + priv->status = STATUS_READ;
  184. + else
  185. + priv->status = STATUS_WRITE;
  186. +
  187. + /* send slave address */
  188. + if (msg->flags & I2C_M_TEN) {
  189. + i2c_w32(0xf0 | ((addr & 0x300) >> 7) | rd, txd);
  190. + i2c_w32(addr & 0xff, txd);
  191. + } else {
  192. + i2c_w32((addr & 0x7f) << 1 | rd, txd);
  193. + }
  194. +}
  195. +
  196. +static void ltq_i2c_set_tx_len(struct ltq_i2c *priv)
  197. +{
  198. + struct i2c_msg *msg = priv->current_msg;
  199. + int len = (msg->flags & I2C_M_TEN) ? 2 : 1;
  200. +
  201. + pr_debug("set_tx_len %cX\n", (msg->flags & I2C_M_RD) ? 'R' : 'T');
  202. +
  203. + priv->status = STATUS_ADDR;
  204. +
  205. + if (!(msg->flags & I2C_M_RD))
  206. + len += msg->len;
  207. + else
  208. + /* set maximum received packet size (before rx int!) */
  209. + i2c_w32(msg->len, mrps_ctrl);
  210. + i2c_w32(len, tps_ctrl);
  211. + enable_burst_irq(priv);
  212. +}
  213. +
  214. +static int ltq_i2c_hw_set_clock(struct i2c_adapter *adap)
  215. +{
  216. + struct ltq_i2c *priv = i2c_get_adapdata(adap);
  217. + unsigned int input_clock = clk_get_rate(priv->clk_input);
  218. + u32 dec, inc = 1;
  219. +
  220. + /* clock changed? */
  221. + if (priv->input_clock == input_clock)
  222. + return 0;
  223. +
  224. + /*
  225. + * this formula is only an approximation, found by the recommended
  226. + * values in the "I2C Architecture Specification 1.7.1"
  227. + */
  228. + dec = input_clock / (priv->i2c_clock * 2);
  229. + if (dec <= 6)
  230. + return -ENXIO;
  231. +
  232. + i2c_w32(0, fdiv_high_cfg);
  233. + i2c_w32((inc << I2C_FDIV_CFG_INC_OFFSET) |
  234. + (dec << I2C_FDIV_CFG_DEC_OFFSET),
  235. + fdiv_cfg);
  236. +
  237. + dev_info(priv->dev, "setup clocks (in %d kHz, bus %d kHz, dec=%d)\n",
  238. + input_clock, priv->i2c_clock, dec);
  239. +
  240. + priv->input_clock = input_clock;
  241. + return 0;
  242. +}
  243. +
  244. +static int ltq_i2c_hw_init(struct i2c_adapter *adap)
  245. +{
  246. + int ret = 0;
  247. + struct ltq_i2c *priv = i2c_get_adapdata(adap);
  248. +
  249. + /* disable bus */
  250. + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
  251. +
  252. +#ifndef DEBUG
  253. + /* set normal operation clock divider */
  254. + i2c_w32(1 << I2C_CLC_RMC_OFFSET, clc);
  255. +#else
  256. + /* for debugging a higher divider value! */
  257. + i2c_w32(0xF0 << I2C_CLC_RMC_OFFSET, clc);
  258. +#endif
  259. +
  260. + /* setup clock */
  261. + ret = ltq_i2c_hw_set_clock(adap);
  262. + if (ret != 0) {
  263. + dev_warn(priv->dev, "invalid clock settings\n");
  264. + return ret;
  265. + }
  266. +
  267. + /* configure fifo */
  268. + i2c_w32(I2C_FIFO_CFG_TXFC | /* tx fifo as flow controller */
  269. + I2C_FIFO_CFG_RXFC | /* rx fifo as flow controller */
  270. + I2C_FIFO_CFG_TXFA_TXFA2 | /* tx fifo 4-byte aligned */
  271. + I2C_FIFO_CFG_RXFA_RXFA2 | /* rx fifo 4-byte aligned */
  272. + I2C_FIFO_CFG_TXBS_TXBS0 | /* tx fifo burst size is 1 word */
  273. + I2C_FIFO_CFG_RXBS_RXBS0, /* rx fifo burst size is 1 word */
  274. + fifo_cfg);
  275. +
  276. + /* configure address */
  277. + i2c_w32(I2C_ADDR_CFG_SOPE_EN | /* generate stop when no more data in
  278. + the fifo */
  279. + I2C_ADDR_CFG_SONA_EN | /* generate stop when NA received */
  280. + I2C_ADDR_CFG_MnS_EN | /* we are master device */
  281. + 0, /* our slave address (not used!) */
  282. + addr_cfg);
  283. +
  284. + /* enable bus */
  285. + i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
  286. +
  287. + return 0;
  288. +}
  289. +
  290. +static int ltq_i2c_wait_bus_not_busy(struct ltq_i2c *priv)
  291. +{
  292. + unsigned long timeout;
  293. +
  294. + timeout = jiffies + msecs_to_jiffies(LTQ_I2C_BUSY_TIMEOUT);
  295. +
  296. + do {
  297. + u32 stat = i2c_r32(bus_stat);
  298. +
  299. + if ((stat & I2C_BUS_STAT_BS_MASK) == I2C_BUS_STAT_BS_FREE)
  300. + return 0;
  301. +
  302. + cond_resched();
  303. + } while (!time_after_eq(jiffies, timeout));
  304. +
  305. + dev_err(priv->dev, "timeout waiting for bus ready\n");
  306. + return -ETIMEDOUT;
  307. +}
  308. +
  309. +static void ltq_i2c_tx(struct ltq_i2c *priv, int last)
  310. +{
  311. + if (priv->msg_buf_len && priv->msg_buf) {
  312. + i2c_w32(*priv->msg_buf, txd);
  313. +
  314. + if (--priv->msg_buf_len)
  315. + priv->msg_buf++;
  316. + else
  317. + priv->msg_buf = NULL;
  318. + } else {
  319. + last = 1;
  320. + }
  321. +
  322. + if (last)
  323. + disable_burst_irq(priv);
  324. +}
  325. +
  326. +static void ltq_i2c_rx(struct ltq_i2c *priv, int last)
  327. +{
  328. + u32 fifo_stat, timeout;
  329. + if (priv->msg_buf_len && priv->msg_buf) {
  330. + timeout = 5000000;
  331. + do {
  332. + fifo_stat = i2c_r32(ffs_stat);
  333. + } while (!fifo_stat && --timeout);
  334. + if (!timeout) {
  335. + last = 1;
  336. + pr_debug("\nrx timeout\n");
  337. + goto err;
  338. + }
  339. + while (fifo_stat) {
  340. + *priv->msg_buf = i2c_r32(rxd);
  341. + if (--priv->msg_buf_len) {
  342. + priv->msg_buf++;
  343. + } else {
  344. + priv->msg_buf = NULL;
  345. + last = 1;
  346. + break;
  347. + }
  348. + /*
  349. + * do not read more than burst size, otherwise no "last
  350. + * burst" is generated and the transaction is blocked!
  351. + */
  352. + fifo_stat = 0;
  353. + }
  354. + } else {
  355. + last = 1;
  356. + }
  357. +err:
  358. + if (last) {
  359. + disable_burst_irq(priv);
  360. +
  361. + if (priv->status == STATUS_READ_END) {
  362. + /*
  363. + * do the STATUS_STOP and complete() here, as sometimes
  364. + * the tx_end is already seen before this is finished
  365. + */
  366. + priv->status = STATUS_STOP;
  367. + complete(&priv->cmd_complete);
  368. + } else {
  369. + i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
  370. + priv->status = STATUS_READ_END;
  371. + }
  372. + }
  373. +}
  374. +
  375. +static void ltq_i2c_xfer_init(struct ltq_i2c *priv)
  376. +{
  377. + /* enable interrupts */
  378. + i2c_w32(LTQ_I2C_IMSC_DEFAULT_MASK, imsc);
  379. +
  380. + /* trigger transfer of first msg */
  381. + ltq_i2c_set_tx_len(priv);
  382. +}
  383. +
  384. +static void dump_msgs(struct i2c_msg msgs[], int num, int rx)
  385. +{
  386. +#if defined(DEBUG)
  387. + int i, j;
  388. + pr_debug("Messages %d %s\n", num, rx ? "out" : "in");
  389. + for (i = 0; i < num; i++) {
  390. + pr_debug("%2d %cX Msg(%d) addr=0x%X: ", i,
  391. + (msgs[i].flags & I2C_M_RD) ? 'R' : 'T',
  392. + msgs[i].len, msgs[i].addr);
  393. + if (!(msgs[i].flags & I2C_M_RD) || rx) {
  394. + for (j = 0; j < msgs[i].len; j++)
  395. + pr_debug("%02X ", msgs[i].buf[j]);
  396. + }
  397. + pr_debug("\n");
  398. + }
  399. +#endif
  400. +}
  401. +
  402. +static void ltq_i2c_release_bus(struct ltq_i2c *priv)
  403. +{
  404. + if ((i2c_r32(bus_stat) & I2C_BUS_STAT_BS_MASK) == I2C_BUS_STAT_BS_BM)
  405. + i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
  406. +}
  407. +
  408. +static int ltq_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  409. + int num)
  410. +{
  411. + struct ltq_i2c *priv = i2c_get_adapdata(adap);
  412. + int ret;
  413. +
  414. + dev_dbg(priv->dev, "xfer %u messages\n", num);
  415. + dump_msgs(msgs, num, 0);
  416. +
  417. + mutex_lock(&priv->mutex);
  418. +
  419. + init_completion(&priv->cmd_complete);
  420. + priv->current_msg = msgs;
  421. + priv->msgs_num = num;
  422. + priv->msg_err = 0;
  423. + priv->status = STATUS_IDLE;
  424. +
  425. + /* wait for the bus to become ready */
  426. + ret = ltq_i2c_wait_bus_not_busy(priv);
  427. + if (ret)
  428. + goto done;
  429. +
  430. + while (priv->msgs_num) {
  431. + /* start the transfers */
  432. + ltq_i2c_xfer_init(priv);
  433. +
  434. + /* wait for transfers to complete */
  435. + ret = wait_for_completion_interruptible_timeout(
  436. + &priv->cmd_complete, LTQ_I2C_XFER_TIMEOUT);
  437. + if (ret == 0) {
  438. + dev_err(priv->dev, "controller timed out\n");
  439. + ltq_i2c_hw_init(adap);
  440. + ret = -ETIMEDOUT;
  441. + goto done;
  442. + } else if (ret < 0)
  443. + goto done;
  444. +
  445. + if (priv->msg_err) {
  446. + if (priv->msg_err & LTQ_I2C_NACK)
  447. + ret = -ENXIO;
  448. + else
  449. + ret = -EREMOTEIO;
  450. + goto done;
  451. + }
  452. + if (--priv->msgs_num)
  453. + priv->current_msg++;
  454. + }
  455. + /* no error? */
  456. + ret = num;
  457. +
  458. +done:
  459. + ltq_i2c_release_bus(priv);
  460. +
  461. + mutex_unlock(&priv->mutex);
  462. +
  463. + if (ret >= 0)
  464. + dump_msgs(msgs, num, 1);
  465. +
  466. + pr_debug("XFER ret %d\n", ret);
  467. + return ret;
  468. +}
  469. +
  470. +static irqreturn_t ltq_i2c_isr_burst(int irq, void *dev_id)
  471. +{
  472. + struct ltq_i2c *priv = dev_id;
  473. + struct i2c_msg *msg = priv->current_msg;
  474. + int last = (irq == priv->irq_lb);
  475. +
  476. + if (last)
  477. + pr_debug("LB ");
  478. + else
  479. + pr_debug("B ");
  480. +
  481. + if (msg->flags & I2C_M_RD) {
  482. + switch (priv->status) {
  483. + case STATUS_ADDR:
  484. + pr_debug("X");
  485. + prepare_msg_send_addr(priv);
  486. + disable_burst_irq(priv);
  487. + break;
  488. + case STATUS_READ:
  489. + case STATUS_READ_END:
  490. + pr_debug("R");
  491. + ltq_i2c_rx(priv, last);
  492. + break;
  493. + default:
  494. + disable_burst_irq(priv);
  495. + pr_warn("Status R %d\n", priv->status);
  496. + break;
  497. + }
  498. + } else {
  499. + switch (priv->status) {
  500. + case STATUS_ADDR:
  501. + pr_debug("x");
  502. + prepare_msg_send_addr(priv);
  503. + break;
  504. + case STATUS_WRITE:
  505. + pr_debug("w");
  506. + ltq_i2c_tx(priv, last);
  507. + break;
  508. + default:
  509. + disable_burst_irq(priv);
  510. + pr_warn("Status W %d\n", priv->status);
  511. + break;
  512. + }
  513. + }
  514. +
  515. + i2c_w32(I2C_ICR_BREQ_INT_CLR | I2C_ICR_LBREQ_INT_CLR, icr);
  516. + return IRQ_HANDLED;
  517. +}
  518. +
  519. +static void ltq_i2c_isr_prot(struct ltq_i2c *priv)
  520. +{
  521. + u32 i_pro = i2c_r32(p_irqss);
  522. +
  523. + pr_debug("i2c-p");
  524. +
  525. + /* not acknowledge */
  526. + if (i_pro & I2C_P_IRQSS_NACK) {
  527. + priv->msg_err |= LTQ_I2C_NACK;
  528. + pr_debug(" nack");
  529. + }
  530. +
  531. + /* arbitration lost */
  532. + if (i_pro & I2C_P_IRQSS_AL) {
  533. + priv->msg_err |= LTQ_I2C_ARB_LOST;
  534. + pr_debug(" arb-lost");
  535. + }
  536. + /* tx -> rx switch */
  537. + if (i_pro & I2C_P_IRQSS_RX)
  538. + pr_debug(" rx");
  539. +
  540. + /* tx end */
  541. + if (i_pro & I2C_P_IRQSS_TX_END)
  542. + pr_debug(" txend");
  543. + pr_debug("\n");
  544. +
  545. + if (!priv->msg_err) {
  546. + /* tx -> rx switch */
  547. + if (i_pro & I2C_P_IRQSS_RX) {
  548. + priv->status = STATUS_READ;
  549. + enable_burst_irq(priv);
  550. + }
  551. + if (i_pro & I2C_P_IRQSS_TX_END) {
  552. + if (priv->status == STATUS_READ)
  553. + priv->status = STATUS_READ_END;
  554. + else {
  555. + disable_burst_irq(priv);
  556. + priv->status = STATUS_STOP;
  557. + }
  558. + }
  559. + }
  560. +
  561. + i2c_w32(i_pro, p_irqsc);
  562. +}
  563. +
  564. +static irqreturn_t ltq_i2c_isr(int irq, void *dev_id)
  565. +{
  566. + u32 i_raw, i_err = 0;
  567. + struct ltq_i2c *priv = dev_id;
  568. +
  569. + i_raw = i2c_r32(mis);
  570. + pr_debug("i_raw 0x%08X\n", i_raw);
  571. +
  572. + /* error interrupt */
  573. + if (i_raw & I2C_RIS_I2C_ERR_INT_INTOCC) {
  574. + i_err = i2c_r32(err_irqss);
  575. + pr_debug("i_err 0x%08X bus_stat 0x%04X\n",
  576. + i_err, i2c_r32(bus_stat));
  577. +
  578. + /* tx fifo overflow (8) */
  579. + if (i_err & I2C_ERR_IRQSS_TXF_OFL)
  580. + priv->msg_err |= LTQ_I2C_TX_OFL;
  581. +
  582. + /* tx fifo underflow (4) */
  583. + if (i_err & I2C_ERR_IRQSS_TXF_UFL)
  584. + priv->msg_err |= LTQ_I2C_TX_UFL;
  585. +
  586. + /* rx fifo overflow (2) */
  587. + if (i_err & I2C_ERR_IRQSS_RXF_OFL)
  588. + priv->msg_err |= LTQ_I2C_RX_OFL;
  589. +
  590. + /* rx fifo underflow (1) */
  591. + if (i_err & I2C_ERR_IRQSS_RXF_UFL)
  592. + priv->msg_err |= LTQ_I2C_RX_UFL;
  593. +
  594. + i2c_w32(i_err, err_irqsc);
  595. + }
  596. +
  597. + /* protocol interrupt */
  598. + if (i_raw & I2C_RIS_I2C_P_INT_INTOCC)
  599. + ltq_i2c_isr_prot(priv);
  600. +
  601. + if ((priv->msg_err) || (priv->status == STATUS_STOP))
  602. + complete(&priv->cmd_complete);
  603. +
  604. + return IRQ_HANDLED;
  605. +}
  606. +
  607. +static u32 ltq_i2c_functionality(struct i2c_adapter *adap)
  608. +{
  609. + return I2C_FUNC_I2C |
  610. + I2C_FUNC_10BIT_ADDR |
  611. + I2C_FUNC_SMBUS_EMUL;
  612. +}
  613. +
  614. +static struct i2c_algorithm ltq_i2c_algorithm = {
  615. + .master_xfer = ltq_i2c_xfer,
  616. + .functionality = ltq_i2c_functionality,
  617. +};
  618. +
  619. +static int ltq_i2c_probe(struct platform_device *pdev)
  620. +{
  621. + struct device_node *node = pdev->dev.of_node;
  622. + struct ltq_i2c *priv;
  623. + struct i2c_adapter *adap;
  624. + struct resource irqres[4];
  625. + int ret = 0;
  626. +
  627. + dev_dbg(&pdev->dev, "probing\n");
  628. +
  629. + ret = of_irq_to_resource_table(node, irqres, 4);
  630. + if (ret != 4) {
  631. + dev_err(&pdev->dev, "no resources\n");
  632. + return -ENODEV;
  633. + }
  634. +
  635. + /* allocate private data */
  636. + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  637. + if (!priv) {
  638. + dev_err(&pdev->dev, "can't allocate private data\n");
  639. + return -ENOMEM;
  640. + }
  641. +
  642. + adap = &priv->adap;
  643. + i2c_set_adapdata(adap, priv);
  644. + adap->owner = THIS_MODULE;
  645. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  646. + strlcpy(adap->name, DRV_NAME "-adapter", sizeof(adap->name));
  647. + adap->algo = &ltq_i2c_algorithm;
  648. + adap->dev.parent = &pdev->dev;
  649. + adap->dev.of_node = pdev->dev.of_node;
  650. +
  651. + if (of_property_read_u32(node, "clock-frequency", &priv->i2c_clock)) {
  652. + dev_warn(&pdev->dev, "No I2C speed selected, using 100kHz\n");
  653. + priv->i2c_clock = 100000;
  654. + }
  655. +
  656. + init_completion(&priv->cmd_complete);
  657. + ret = devm_mutex_init(&pdev->dev, &priv->mutex);
  658. + if (ret)
  659. + return ret;
  660. +
  661. + priv->membase = devm_platform_ioremap_resource(pdev, 0);
  662. + if (IS_ERR(priv->membase))
  663. + return PTR_ERR(priv->membase);
  664. +
  665. + priv->dev = &pdev->dev;
  666. + priv->irq_lb = irqres[0].start;
  667. +
  668. + ret = devm_request_irq(&pdev->dev, irqres[0].start, ltq_i2c_isr_burst,
  669. + 0x0, "i2c lb", priv);
  670. + if (ret) {
  671. + dev_err(&pdev->dev, "can't get last burst IRQ %d\n",
  672. + irqres[0].start);
  673. + return -ENODEV;
  674. + }
  675. +
  676. + ret = devm_request_irq(&pdev->dev, irqres[1].start, ltq_i2c_isr_burst,
  677. + 0x0, "i2c b", priv);
  678. + if (ret) {
  679. + dev_err(&pdev->dev, "can't get burst IRQ %d\n",
  680. + irqres[1].start);
  681. + return -ENODEV;
  682. + }
  683. +
  684. + ret = devm_request_irq(&pdev->dev, irqres[2].start, ltq_i2c_isr,
  685. + 0x0, "i2c err", priv);
  686. + if (ret) {
  687. + dev_err(&pdev->dev, "can't get error IRQ %d\n",
  688. + irqres[2].start);
  689. + return -ENODEV;
  690. + }
  691. +
  692. + ret = devm_request_irq(&pdev->dev, irqres[3].start, ltq_i2c_isr,
  693. + 0x0, "i2c p", priv);
  694. + if (ret) {
  695. + dev_err(&pdev->dev, "can't get protocol IRQ %d\n",
  696. + irqres[3].start);
  697. + return -ENODEV;
  698. + }
  699. +
  700. + dev_dbg(&pdev->dev, "mapped io-space to %p\n", priv->membase);
  701. + dev_dbg(&pdev->dev, "use IRQs %d, %d, %d, %d\n", irqres[0].start,
  702. + irqres[1].start, irqres[2].start, irqres[3].start);
  703. +
  704. + priv->clk_gate = devm_clk_get(&pdev->dev, NULL);
  705. + if (IS_ERR(priv->clk_gate)) {
  706. + dev_err(&pdev->dev, "failed to get i2c clk\n");
  707. + return -ENOENT;
  708. + }
  709. +
  710. + /* this is a static clock, which has no refcounting */
  711. + priv->clk_input = clk_get_fpi();
  712. + if (IS_ERR(priv->clk_input)) {
  713. + dev_err(&pdev->dev, "failed to get fpi clk\n");
  714. + return -ENOENT;
  715. + }
  716. +
  717. + clk_activate(priv->clk_gate);
  718. +
  719. + /* add our adapter to the i2c stack */
  720. + ret = devm_i2c_add_adapter(&pdev->dev, adap);
  721. + if (ret) {
  722. + dev_err(&pdev->dev, "can't register I2C adapter\n");
  723. + goto out;
  724. + }
  725. +
  726. + platform_set_drvdata(pdev, priv);
  727. + i2c_set_adapdata(adap, priv);
  728. +
  729. + /* print module version information */
  730. + dev_dbg(&pdev->dev, "module id=%u revision=%u\n",
  731. + (i2c_r32(id) & I2C_ID_ID_MASK) >> I2C_ID_ID_OFFSET,
  732. + (i2c_r32(id) & I2C_ID_REV_MASK) >> I2C_ID_REV_OFFSET);
  733. +
  734. + /* initialize HW */
  735. + ret = ltq_i2c_hw_init(adap);
  736. + if (ret) {
  737. + dev_err(&pdev->dev, "can't configure adapter\n");
  738. + platform_set_drvdata(pdev, NULL);
  739. + goto out;
  740. + } else {
  741. + dev_info(&pdev->dev, "version %s\n", DRV_VERSION);
  742. + }
  743. +
  744. +out:
  745. + /* if init failed, we need to deactivate the clock gate */
  746. + if (ret)
  747. + clk_deactivate(priv->clk_gate);
  748. +
  749. + return ret;
  750. +}
  751. +
  752. +static int ltq_i2c_remove(struct platform_device *pdev)
  753. +{
  754. + struct ltq_i2c *priv = platform_get_drvdata(pdev);
  755. +
  756. + /* disable bus */
  757. + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
  758. +
  759. + /* power down the core */
  760. + clk_deactivate(priv->clk_gate);
  761. +
  762. + dev_dbg(&pdev->dev, "removed\n");
  763. + platform_set_drvdata(pdev, NULL);
  764. +
  765. + return 0;
  766. +}
  767. +static const struct of_device_id ltq_i2c_match[] = {
  768. + { .compatible = "lantiq,lantiq-i2c" },
  769. + {},
  770. +};
  771. +MODULE_DEVICE_TABLE(of, ltq_i2c_match);
  772. +
  773. +static struct platform_driver ltq_i2c_driver = {
  774. + .probe = ltq_i2c_probe,
  775. + .remove = ltq_i2c_remove,
  776. + .driver = {
  777. + .name = DRV_NAME,
  778. + .of_match_table = ltq_i2c_match,
  779. + },
  780. +};
  781. +
  782. +module_platform_driver(ltq_i2c_driver);
  783. +
  784. +MODULE_DESCRIPTION("Lantiq I2C bus adapter");
  785. +MODULE_AUTHOR("Thomas Langer <[email protected]>");
  786. +MODULE_ALIAS("platform:" DRV_NAME);
  787. +MODULE_LICENSE("GPL");
  788. +MODULE_VERSION(DRV_VERSION);
  789. --- /dev/null
  790. +++ b/drivers/i2c/busses/i2c-lantiq.h
  791. @@ -0,0 +1,234 @@
  792. +#ifndef I2C_LANTIQ_H
  793. +#define I2C_LANTIQ_H
  794. +
  795. +/* I2C register structure */
  796. +struct lantiq_reg_i2c {
  797. + /* I2C Kernel Clock Control Register */
  798. + unsigned int clc; /* 0x00000000 */
  799. + /* Reserved */
  800. + unsigned int res_0; /* 0x00000004 */
  801. + /* I2C Identification Register */
  802. + unsigned int id; /* 0x00000008 */
  803. + /* Reserved */
  804. + unsigned int res_1; /* 0x0000000C */
  805. + /*
  806. + * I2C RUN Control Register
  807. + * This register enables and disables the I2C peripheral. Before
  808. + * enabling, the I2C has to be configured properly. After enabling
  809. + * no configuration is possible
  810. + */
  811. + unsigned int run_ctrl; /* 0x00000010 */
  812. + /*
  813. + * I2C End Data Control Register
  814. + * This register is used to either turn around the data transmission
  815. + * direction or to address another slave without sending a stop
  816. + * condition. Also the software can stop the slave-transmitter by
  817. + * sending a not-accolade when working as master-receiver or even
  818. + * stop data transmission immediately when operating as
  819. + * master-transmitter. The writing to the bits of this control
  820. + * register is only effective when in MASTER RECEIVES BYTES, MASTER
  821. + * TRANSMITS BYTES, MASTER RESTART or SLAVE RECEIVE BYTES state
  822. + */
  823. + unsigned int endd_ctrl; /* 0x00000014 */
  824. + /*
  825. + * I2C Fractional Divider Configuration Register
  826. + * These register is used to program the fractional divider of the I2C
  827. + * bus. Before the peripheral is switched on by setting the RUN-bit the
  828. + * two (fixed) values for the two operating frequencies are programmed
  829. + * into these (configuration) registers. The Register FDIV_HIGH_CFG has
  830. + * the same layout as I2C_FDIV_CFG.
  831. + */
  832. + unsigned int fdiv_cfg; /* 0x00000018 */
  833. + /*
  834. + * I2C Fractional Divider (highspeed mode) Configuration Register
  835. + * These register is used to program the fractional divider of the I2C
  836. + * bus. Before the peripheral is switched on by setting the RUN-bit the
  837. + * two (fixed) values for the two operating frequencies are programmed
  838. + * into these (configuration) registers. The Register FDIV_CFG has the
  839. + * same layout as I2C_FDIV_CFG.
  840. + */
  841. + unsigned int fdiv_high_cfg; /* 0x0000001C */
  842. + /* I2C Address Configuration Register */
  843. + unsigned int addr_cfg; /* 0x00000020 */
  844. + /* I2C Bus Status Register
  845. + * This register gives a status information of the I2C. This additional
  846. + * information can be used by the software to start proper actions.
  847. + */
  848. + unsigned int bus_stat; /* 0x00000024 */
  849. + /* I2C FIFO Configuration Register */
  850. + unsigned int fifo_cfg; /* 0x00000028 */
  851. + /* I2C Maximum Received Packet Size Register */
  852. + unsigned int mrps_ctrl; /* 0x0000002C */
  853. + /* I2C Received Packet Size Status Register */
  854. + unsigned int rps_stat; /* 0x00000030 */
  855. + /* I2C Transmit Packet Size Register */
  856. + unsigned int tps_ctrl; /* 0x00000034 */
  857. + /* I2C Filled FIFO Stages Status Register */
  858. + unsigned int ffs_stat; /* 0x00000038 */
  859. + /* Reserved */
  860. + unsigned int res_2; /* 0x0000003C */
  861. + /* I2C Timing Configuration Register */
  862. + unsigned int tim_cfg; /* 0x00000040 */
  863. + /* Reserved */
  864. + unsigned int res_3[7]; /* 0x00000044 */
  865. + /* I2C Error Interrupt Request Source Mask Register */
  866. + unsigned int err_irqsm; /* 0x00000060 */
  867. + /* I2C Error Interrupt Request Source Status Register */
  868. + unsigned int err_irqss; /* 0x00000064 */
  869. + /* I2C Error Interrupt Request Source Clear Register */
  870. + unsigned int err_irqsc; /* 0x00000068 */
  871. + /* Reserved */
  872. + unsigned int res_4; /* 0x0000006C */
  873. + /* I2C Protocol Interrupt Request Source Mask Register */
  874. + unsigned int p_irqsm; /* 0x00000070 */
  875. + /* I2C Protocol Interrupt Request Source Status Register */
  876. + unsigned int p_irqss; /* 0x00000074 */
  877. + /* I2C Protocol Interrupt Request Source Clear Register */
  878. + unsigned int p_irqsc; /* 0x00000078 */
  879. + /* Reserved */
  880. + unsigned int res_5; /* 0x0000007C */
  881. + /* I2C Raw Interrupt Status Register */
  882. + unsigned int ris; /* 0x00000080 */
  883. + /* I2C Interrupt Mask Control Register */
  884. + unsigned int imsc; /* 0x00000084 */
  885. + /* I2C Masked Interrupt Status Register */
  886. + unsigned int mis; /* 0x00000088 */
  887. + /* I2C Interrupt Clear Register */
  888. + unsigned int icr; /* 0x0000008C */
  889. + /* I2C Interrupt Set Register */
  890. + unsigned int isr; /* 0x00000090 */
  891. + /* I2C DMA Enable Register */
  892. + unsigned int dmae; /* 0x00000094 */
  893. + /* Reserved */
  894. + unsigned int res_6[8154]; /* 0x00000098 */
  895. + /* I2C Transmit Data Register */
  896. + unsigned int txd; /* 0x00008000 */
  897. + /* Reserved */
  898. + unsigned int res_7[4095]; /* 0x00008004 */
  899. + /* I2C Receive Data Register */
  900. + unsigned int rxd; /* 0x0000C000 */
  901. + /* Reserved */
  902. + unsigned int res_8[4095]; /* 0x0000C004 */
  903. +};
  904. +
  905. +/*
  906. + * Clock Divider for Normal Run Mode
  907. + * Max 8-bit divider value. IF RMC is 0 the module is disabled. Note: As long
  908. + * as the new divider value RMC is not valid, the register returns 0x0000 00xx
  909. + * on reading.
  910. + */
  911. +#define I2C_CLC_RMC_MASK 0x0000FF00
  912. +/* field offset */
  913. +#define I2C_CLC_RMC_OFFSET 8
  914. +
  915. +/* Fields of "I2C Identification Register" */
  916. +/* Module ID */
  917. +#define I2C_ID_ID_MASK 0x0000FF00
  918. +/* field offset */
  919. +#define I2C_ID_ID_OFFSET 8
  920. +/* Revision */
  921. +#define I2C_ID_REV_MASK 0x000000FF
  922. +/* field offset */
  923. +#define I2C_ID_REV_OFFSET 0
  924. +
  925. +/* Fields of "I2C Interrupt Mask Control Register" */
  926. +/* Enable */
  927. +#define I2C_IMSC_BREQ_INT_EN 0x00000008
  928. +/* Enable */
  929. +#define I2C_IMSC_LBREQ_INT_EN 0x00000004
  930. +
  931. +/* Fields of "I2C Fractional Divider Configuration Register" */
  932. +/* field offset */
  933. +#define I2C_FDIV_CFG_INC_OFFSET 16
  934. +
  935. +/* Fields of "I2C Interrupt Mask Control Register" */
  936. +/* Enable */
  937. +#define I2C_IMSC_I2C_P_INT_EN 0x00000020
  938. +/* Enable */
  939. +#define I2C_IMSC_I2C_ERR_INT_EN 0x00000010
  940. +
  941. +/* Fields of "I2C Error Interrupt Request Source Status Register" */
  942. +/* TXF_OFL */
  943. +#define I2C_ERR_IRQSS_TXF_OFL 0x00000008
  944. +/* TXF_UFL */
  945. +#define I2C_ERR_IRQSS_TXF_UFL 0x00000004
  946. +/* RXF_OFL */
  947. +#define I2C_ERR_IRQSS_RXF_OFL 0x00000002
  948. +/* RXF_UFL */
  949. +#define I2C_ERR_IRQSS_RXF_UFL 0x00000001
  950. +
  951. +/* Fields of "I2C Raw Interrupt Status Register" */
  952. +/* Read: Interrupt occurred. */
  953. +#define I2C_RIS_I2C_ERR_INT_INTOCC 0x00000010
  954. +/* Read: Interrupt occurred. */
  955. +#define I2C_RIS_I2C_P_INT_INTOCC 0x00000020
  956. +
  957. +/* Fields of "I2C FIFO Configuration Register" */
  958. +/* TX FIFO Flow Control */
  959. +#define I2C_FIFO_CFG_TXFC 0x00020000
  960. +/* RX FIFO Flow Control */
  961. +#define I2C_FIFO_CFG_RXFC 0x00010000
  962. +/* Word aligned (character alignment of four characters) */
  963. +#define I2C_FIFO_CFG_TXFA_TXFA2 0x00002000
  964. +/* Word aligned (character alignment of four characters) */
  965. +#define I2C_FIFO_CFG_RXFA_RXFA2 0x00000200
  966. +/* 1 word */
  967. +#define I2C_FIFO_CFG_TXBS_TXBS0 0x00000000
  968. +
  969. +/* Fields of "I2C FIFO Configuration Register" */
  970. +/* 1 word */
  971. +#define I2C_FIFO_CFG_RXBS_RXBS0 0x00000000
  972. +/* Stop on Packet End Enable */
  973. +#define I2C_ADDR_CFG_SOPE_EN 0x00200000
  974. +/* Stop on Not Acknowledge Enable */
  975. +#define I2C_ADDR_CFG_SONA_EN 0x00100000
  976. +/* Enable */
  977. +#define I2C_ADDR_CFG_MnS_EN 0x00080000
  978. +
  979. +/* Fields of "I2C Interrupt Clear Register" */
  980. +/* Clear */
  981. +#define I2C_ICR_BREQ_INT_CLR 0x00000008
  982. +/* Clear */
  983. +#define I2C_ICR_LBREQ_INT_CLR 0x00000004
  984. +
  985. +/* Fields of "I2C Fractional Divider Configuration Register" */
  986. +/* field offset */
  987. +#define I2C_FDIV_CFG_DEC_OFFSET 0
  988. +
  989. +/* Fields of "I2C Bus Status Register" */
  990. +/* Bus Status */
  991. +#define I2C_BUS_STAT_BS_MASK 0x00000003
  992. +/* Read from I2C Bus. */
  993. +#define I2C_BUS_STAT_RNW_READ 0x00000004
  994. +/* I2C Bus is free. */
  995. +#define I2C_BUS_STAT_BS_FREE 0x00000000
  996. +/*
  997. + * The device is working as master and has claimed the control on the
  998. + * I2C-bus (busy master).
  999. + */
  1000. +#define I2C_BUS_STAT_BS_BM 0x00000002
  1001. +
  1002. +/* Fields of "I2C RUN Control Register" */
  1003. +/* Enable */
  1004. +#define I2C_RUN_CTRL_RUN_EN 0x00000001
  1005. +
  1006. +/* Fields of "I2C End Data Control Register" */
  1007. +/*
  1008. + * Set End of Transmission
  1009. + * Note:Do not write '1' to this bit when bus is free. This will cause an
  1010. + * abort after the first byte when a new transfer is started.
  1011. + */
  1012. +#define I2C_ENDD_CTRL_SETEND 0x00000002
  1013. +
  1014. +/* Fields of "I2C Protocol Interrupt Request Source Status Register" */
  1015. +/* NACK */
  1016. +#define I2C_P_IRQSS_NACK 0x00000010
  1017. +/* AL */
  1018. +#define I2C_P_IRQSS_AL 0x00000008
  1019. +/* RX */
  1020. +#define I2C_P_IRQSS_RX 0x00000040
  1021. +/* TX_END */
  1022. +#define I2C_P_IRQSS_TX_END 0x00000020
  1023. +
  1024. +
  1025. +#endif /* I2C_LANTIQ_H */