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

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