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. +
  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 *mmres, irqres[4];
  625. + int ret = 0;
  626. +
  627. + dev_dbg(&pdev->dev, "probing\n");
  628. +
  629. + mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  630. + ret = of_irq_to_resource_table(node, irqres, 4);
  631. + if (!mmres || (ret != 4)) {
  632. + dev_err(&pdev->dev, "no resources\n");
  633. + return -ENODEV;
  634. + }
  635. +
  636. + /* allocate private data */
  637. + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  638. + if (!priv) {
  639. + dev_err(&pdev->dev, "can't allocate private data\n");
  640. + return -ENOMEM;
  641. + }
  642. +
  643. + adap = &priv->adap;
  644. + i2c_set_adapdata(adap, priv);
  645. + adap->owner = THIS_MODULE;
  646. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  647. + strlcpy(adap->name, DRV_NAME "-adapter", sizeof(adap->name));
  648. + adap->algo = &ltq_i2c_algorithm;
  649. + adap->dev.parent = &pdev->dev;
  650. + adap->dev.of_node = pdev->dev.of_node;
  651. +
  652. + if (of_property_read_u32(node, "clock-frequency", &priv->i2c_clock)) {
  653. + dev_warn(&pdev->dev, "No I2C speed selected, using 100kHz\n");
  654. + priv->i2c_clock = 100000;
  655. + }
  656. +
  657. + init_completion(&priv->cmd_complete);
  658. + mutex_init(&priv->mutex);
  659. +
  660. + priv->membase = devm_ioremap_resource(&pdev->dev, mmres);
  661. + if (IS_ERR(priv->membase))
  662. + return PTR_ERR(priv->membase);
  663. +
  664. + priv->dev = &pdev->dev;
  665. + priv->irq_lb = irqres[0].start;
  666. +
  667. + ret = devm_request_irq(&pdev->dev, irqres[0].start, ltq_i2c_isr_burst,
  668. + 0x0, "i2c lb", priv);
  669. + if (ret) {
  670. + dev_err(&pdev->dev, "can't get last burst IRQ %d\n",
  671. + irqres[0].start);
  672. + return -ENODEV;
  673. + }
  674. +
  675. + ret = devm_request_irq(&pdev->dev, irqres[1].start, ltq_i2c_isr_burst,
  676. + 0x0, "i2c b", priv);
  677. + if (ret) {
  678. + dev_err(&pdev->dev, "can't get burst IRQ %d\n",
  679. + irqres[1].start);
  680. + return -ENODEV;
  681. + }
  682. +
  683. + ret = devm_request_irq(&pdev->dev, irqres[2].start, ltq_i2c_isr,
  684. + 0x0, "i2c err", priv);
  685. + if (ret) {
  686. + dev_err(&pdev->dev, "can't get error IRQ %d\n",
  687. + irqres[2].start);
  688. + return -ENODEV;
  689. + }
  690. +
  691. + ret = devm_request_irq(&pdev->dev, irqres[3].start, ltq_i2c_isr,
  692. + 0x0, "i2c p", priv);
  693. + if (ret) {
  694. + dev_err(&pdev->dev, "can't get protocol IRQ %d\n",
  695. + irqres[3].start);
  696. + return -ENODEV;
  697. + }
  698. +
  699. + dev_dbg(&pdev->dev, "mapped io-space to %p\n", priv->membase);
  700. + dev_dbg(&pdev->dev, "use IRQs %d, %d, %d, %d\n", irqres[0].start,
  701. + irqres[1].start, irqres[2].start, irqres[3].start);
  702. +
  703. + priv->clk_gate = devm_clk_get(&pdev->dev, NULL);
  704. + if (IS_ERR(priv->clk_gate)) {
  705. + dev_err(&pdev->dev, "failed to get i2c clk\n");
  706. + return -ENOENT;
  707. + }
  708. +
  709. + /* this is a static clock, which has no refcounting */
  710. + priv->clk_input = clk_get_fpi();
  711. + if (IS_ERR(priv->clk_input)) {
  712. + dev_err(&pdev->dev, "failed to get fpi clk\n");
  713. + return -ENOENT;
  714. + }
  715. +
  716. + clk_activate(priv->clk_gate);
  717. +
  718. + /* add our adapter to the i2c stack */
  719. + ret = i2c_add_numbered_adapter(adap);
  720. + if (ret) {
  721. + dev_err(&pdev->dev, "can't register I2C adapter\n");
  722. + goto out;
  723. + }
  724. +
  725. + platform_set_drvdata(pdev, priv);
  726. + i2c_set_adapdata(adap, priv);
  727. +
  728. + /* print module version information */
  729. + dev_dbg(&pdev->dev, "module id=%u revision=%u\n",
  730. + (i2c_r32(id) & I2C_ID_ID_MASK) >> I2C_ID_ID_OFFSET,
  731. + (i2c_r32(id) & I2C_ID_REV_MASK) >> I2C_ID_REV_OFFSET);
  732. +
  733. + /* initialize HW */
  734. + ret = ltq_i2c_hw_init(adap);
  735. + if (ret) {
  736. + dev_err(&pdev->dev, "can't configure adapter\n");
  737. + i2c_del_adapter(adap);
  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. + /* 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 = 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 */