120-falcon-i2c.patch 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. --- a/drivers/i2c/busses/Makefile
  2. +++ b/drivers/i2c/busses/Makefile
  3. @@ -74,6 +74,7 @@
  4. obj-$(CONFIG_I2C_STUB) += i2c-stub.o
  5. obj-$(CONFIG_SCx200_ACB) += scx200_acb.o
  6. obj-$(CONFIG_SCx200_I2C) += scx200_i2c.o
  7. +obj-$(CONFIG_I2C_FALCON) += i2c-falcon.o
  8. ifeq ($(CONFIG_I2C_DEBUG_BUS),y)
  9. EXTRA_CFLAGS += -DDEBUG
  10. --- a/drivers/i2c/busses/Kconfig
  11. +++ b/drivers/i2c/busses/Kconfig
  12. @@ -278,6 +278,10 @@
  13. comment "I2C system bus drivers (mostly embedded / system-on-chip)"
  14. +config I2C_FALCON
  15. + tristate "Falcon I2C interface"
  16. +# depends on SOC_FALCON
  17. +
  18. config I2C_AT91
  19. tristate "Atmel AT91 I2C Two-Wire interface (TWI)"
  20. depends on ARCH_AT91 && EXPERIMENTAL && BROKEN
  21. --- /dev/null
  22. +++ b/drivers/i2c/busses/i2c-falcon.c
  23. @@ -0,0 +1,803 @@
  24. +/*
  25. + * This program is free software; you can redistribute it and/or modify
  26. + * it under the terms of the GNU General Public License as published by
  27. + * the Free Software Foundation; either version 2 of the License, or
  28. + * (at your option) any later version.
  29. + *
  30. + * This program is distributed in the hope that it will be useful,
  31. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. + * GNU General Public License for more details.
  34. + *
  35. + * You should have received a copy of the GNU General Public License
  36. + * along with this program; if not, write to the Free Software
  37. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  38. + */
  39. +
  40. +/* #define DEBUG */
  41. +
  42. +#include <linux/module.h>
  43. +#include <linux/ioport.h>
  44. +#include <linux/init.h>
  45. +#include <linux/platform_device.h>
  46. +#include <linux/i2c.h>
  47. +#include <linux/interrupt.h>
  48. +#include <linux/spinlock.h>
  49. +#include <linux/io.h>
  50. +#include <linux/clk.h>
  51. +#include <linux/err.h>
  52. +#include <linux/slab.h>
  53. +
  54. +/* CURRENT ISSUES:
  55. + * - no high speed support
  56. + * - rx issue with "address mode" & "tx end" interrupts
  57. + * - ten bit mode is not tested
  58. + */
  59. +
  60. +/* mapping for access macros */
  61. +#define reg_r32(reg) __raw_readl(reg)
  62. +#define reg_w32(val, reg) __raw_writel(val, reg)
  63. +#define reg_w32_mask(clear, set, reg) \
  64. + reg_w32((reg_r32(reg) & ~(clear)) | (set), reg)
  65. +#define reg_r32_table(reg, idx) reg_r32(&((uint32_t *)&reg)[idx])
  66. +#define reg_w32_table(val, reg, idx) reg_w32(val, &((uint32_t *)&reg)[idx])
  67. +#define i2c (priv->membase)
  68. +#include <falcon/i2c_reg.h>
  69. +
  70. +/* enable hacks to overcome current issue */
  71. +#define FALCON_FIX_ME
  72. +
  73. +#define FALCON_I2C_ADDR 0x00
  74. +#define FALCON_I2C_READY_TIMEOUT 1000
  75. +#define FALCON_I2C_WAIT_TIMEOUT 10
  76. +
  77. +#define DRV_NAME "i2c-falcon"
  78. +
  79. +#if defined(DEBUG)
  80. +#define static /* no static functions for better debugging */
  81. +#endif
  82. +
  83. +#define FALCON_I2C_ARB_LOST (1 << 0)
  84. +#define FALCON_I2C_NACK (1 << 1)
  85. +#define FALCON_I2C_RX_UFL (1 << 2)
  86. +#define FALCON_I2C_RX_OFL (1 << 3)
  87. +#define FALCON_I2C_TX_UFL (1 << 4)
  88. +#define FALCON_I2C_TX_OFL (1 << 5)
  89. +#define FALCON_I2C_BURST_REQ (1 << 6)
  90. +#define FALCON_I2C_RX (1 << 7)
  91. +#define FALCON_I2C_TX_END (1 << 8)
  92. +#define FALCON_I2C_ADDR_MATCH (1 << 9) /* doesn't trigger */
  93. +
  94. +struct falcon_i2c {
  95. + spinlock_t lock;
  96. +
  97. + enum {
  98. + FALCON_I2C_MODE_100 = 1,
  99. + FALCON_I2C_MODE_400 = 2,
  100. + FALCON_I2C_MODE_3400 = 3
  101. + } mode; /* current speed mode */
  102. +
  103. + int ten_bit; /* current address mode */
  104. + unsigned long status; /* bus events holder */
  105. + struct clk *clk; /* clock input for i2c hardware block */
  106. + struct gpon_reg_i2c __iomem *membase; /* base of mapped registers */
  107. + int irq_lb, irq_b, irq_err, irq_p; /* last burst, burst, error,
  108. + protocol IRQs */
  109. + struct completion done;
  110. + struct i2c_adapter adap;
  111. + struct device *dev;
  112. +};
  113. +
  114. +#define FALCON_I2C_ERROR_MASK (FALCON_I2C_NACK \
  115. + | FALCON_I2C_ARB_LOST \
  116. + | FALCON_I2C_RX_OFL \
  117. + | FALCON_I2C_RX_UFL \
  118. + | FALCON_I2C_TX_OFL \
  119. + | FALCON_I2C_TX_UFL)
  120. +
  121. +#define FALCON_I2C_ERROR(priv) (priv->status & FALCON_I2C_ERROR_MASK)
  122. +#define FALCON_I2C_ERROR_CLEAR(priv) do { \
  123. + priv->status &= \
  124. + ~FALCON_I2C_ERROR_MASK; \
  125. + } while (0)
  126. +
  127. +static void falcon_addr_configure(struct falcon_i2c *priv, int ten_bit)
  128. +{
  129. + u32 ten_bit_mask = ten_bit ? I2C_ADDR_CFG_TBAM_10bit : 0;
  130. +
  131. + /* configure address */
  132. + i2c_w32(I2C_ADDR_CFG_SOPE_EN /* generate stop when no more data in the
  133. + fifo */
  134. + | I2C_ADDR_CFG_SONA_EN /* generate stop when NA received */
  135. + | I2C_ADDR_CFG_MnS_EN /* we are master device */
  136. + | ten_bit_mask
  137. + | FALCON_I2C_ADDR, /* our address */
  138. + addr_cfg);
  139. +}
  140. +
  141. +static irqreturn_t falcon_i2c_isr(int irq, void *dev_id)
  142. +{
  143. + u32 i_raw, i_pro, i_err;
  144. + struct falcon_i2c *priv = dev_id;
  145. + unsigned long flags;
  146. + unsigned int old_status;
  147. +
  148. + spin_lock_irqsave(&priv->lock, flags);
  149. +
  150. + old_status = (unsigned int)priv->status;
  151. +
  152. + i_raw = i2c_r32(mis);
  153. +
  154. + /* protocol interrupt */
  155. + if (i_raw & I2C_RIS_I2C_P_INT_INTOCC) {
  156. + i_pro = i2c_r32(p_irqss);
  157. +
  158. + /* tx -> rx switch */
  159. + if (i_pro & I2C_P_IRQSS_RX)
  160. + priv->status |= FALCON_I2C_RX;
  161. +
  162. + /* tx end */
  163. + if (i_pro & I2C_P_IRQSS_TX_END)
  164. + priv->status |= FALCON_I2C_TX_END;
  165. +
  166. + /* not acknowledge */
  167. + if (i_pro & I2C_P_IRQSS_NACK)
  168. + priv->status |= FALCON_I2C_NACK;
  169. +
  170. + /* arbitration lost */
  171. + if (i_pro & I2C_P_IRQSS_AL)
  172. + priv->status |= FALCON_I2C_ARB_LOST;
  173. +
  174. + /* address match */
  175. + if (i_pro & I2C_P_IRQSS_AM)
  176. + priv->status |= FALCON_I2C_ADDR_MATCH;
  177. +
  178. + i2c_w32(i_pro, p_irqsc);
  179. + }
  180. +
  181. + /* error interrupt */
  182. + if (i_raw & I2C_RIS_I2C_ERR_INT_INTOCC) {
  183. + i_err = i2c_r32(err_irqss);
  184. +
  185. + /* tx fifo overflow */
  186. + if (i_err & I2C_ERR_IRQSS_TXF_OFL)
  187. + priv->status |= FALCON_I2C_TX_OFL;
  188. +
  189. + /* tx fifo underflow */
  190. + if (i_err & I2C_ERR_IRQSS_TXF_UFL)
  191. + priv->status |= FALCON_I2C_TX_UFL;
  192. +
  193. + /* rx fifo overflow */
  194. + if (i_err & I2C_ERR_IRQSS_RXF_OFL)
  195. + priv->status |= FALCON_I2C_RX_OFL;
  196. +
  197. + /* rx fifo underflow */
  198. + if (i_err & I2C_ERR_IRQSS_RXF_UFL)
  199. + priv->status |= FALCON_I2C_RX_UFL;
  200. +
  201. + i2c_w32(i_err, err_irqsc);
  202. + }
  203. +
  204. + /* burst request */
  205. + if (i_raw & I2C_RIS_BREQ_INT_INTOCC) {
  206. + i2c_w32_mask(I2C_IMSC_BREQ_INT_EN, 0, imsc);
  207. + i2c_w32_mask(0, I2C_ICR_BREQ_INT_CLR, icr);
  208. +
  209. + priv->status |= FALCON_I2C_BURST_REQ;
  210. + }
  211. +
  212. + /* last burst request */
  213. + if (i_raw & I2C_RIS_LBREQ_INT_INTOCC) {
  214. + i2c_w32_mask(I2C_IMSC_LBREQ_INT_EN, 0, imsc);
  215. + i2c_w32_mask(0, I2C_ICR_LBREQ_INT_CLR, icr);
  216. +
  217. + priv->status |= FALCON_I2C_BURST_REQ;
  218. + }
  219. +
  220. + if (old_status != (unsigned int)priv->status)
  221. + complete(&priv->done);
  222. +
  223. + spin_unlock_irqrestore(&priv->lock, flags);
  224. +
  225. + return IRQ_HANDLED;
  226. +}
  227. +
  228. +static int falcon_i2c_ready(struct falcon_i2c *priv)
  229. +{
  230. + int timeout;
  231. + u32 bus_stat;
  232. + unsigned long flags;
  233. + int ret;
  234. +
  235. + for (timeout = 0; timeout < FALCON_I2C_READY_TIMEOUT; timeout++) {
  236. + bus_stat = i2c_r32(bus_stat);
  237. +
  238. + if (bus_stat & I2C_BUS_STAT_BS_SC) {
  239. + cpu_relax();
  240. + } else {
  241. + spin_lock_irqsave(&priv->lock, flags);
  242. +
  243. + if (FALCON_I2C_ERROR(priv)) {
  244. + ret = priv->status;
  245. +
  246. + dev_dbg(priv->dev, "bus ready wait error 0x%08lx\n", priv->status);
  247. +
  248. + FALCON_I2C_ERROR_CLEAR(priv);
  249. + } else {
  250. + ret = 0;
  251. + }
  252. +
  253. + spin_unlock_irqrestore(&priv->lock, flags);
  254. +
  255. + return ret;
  256. + }
  257. + }
  258. +
  259. + dev_dbg(priv->dev, "bus ready wait timeout\n");
  260. +
  261. + return -ETIME;
  262. +}
  263. +
  264. +static int falcon_i2c_wait(struct falcon_i2c *priv, unsigned long status)
  265. +{
  266. + int ret = 0;
  267. + unsigned long flags;
  268. + unsigned int timeout;
  269. +
  270. + spin_lock_irqsave(&priv->lock, flags);
  271. +
  272. + priv->status &= FALCON_I2C_BURST_REQ;
  273. +
  274. + /* check if the event already occurred */
  275. + if ((priv->status & status) == status) {
  276. + priv->status &= ~status;
  277. + spin_unlock_irqrestore(&priv->lock, flags);
  278. +
  279. + return 0;
  280. + }
  281. +
  282. + spin_unlock_irqrestore(&priv->lock, flags);
  283. +
  284. + /* unmask burst interrupts */
  285. + i2c_w32_mask(0, I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, imsc);
  286. +
  287. + for (timeout = 0; timeout < FALCON_I2C_WAIT_TIMEOUT; timeout++) {
  288. + /* wait for the data request */
  289. + wait_for_completion_timeout(&priv->done, HZ / 10);
  290. +
  291. + /* handle errors */
  292. + spin_lock_irqsave(&priv->lock, flags);
  293. +
  294. + if (FALCON_I2C_ERROR(priv)) {
  295. + dev_dbg(priv->dev, "wait error 0x%08lx (waiting for 0x%08lx)\n",
  296. + priv->status,
  297. + status);
  298. +
  299. + if (priv->status & FALCON_I2C_NACK)
  300. + ret = -ENXIO;
  301. + else
  302. + ret = -EREMOTEIO;
  303. +
  304. + FALCON_I2C_ERROR_CLEAR(priv);
  305. + } else {
  306. + if ((priv->status & status) == status) {
  307. + priv->status &= ~status;
  308. + spin_unlock_irqrestore(&priv->lock, flags);
  309. +
  310. + return 0;
  311. + }
  312. + }
  313. +
  314. + spin_unlock_irqrestore(&priv->lock, flags);
  315. +
  316. + if (ret)
  317. + return ret;
  318. + }
  319. +
  320. + dev_dbg(priv->dev, "wait timeout error 0x%08lx (waiting for 0x%08lx)\n",
  321. + priv->status,
  322. + status);
  323. +
  324. + return -ETIME;
  325. +}
  326. +
  327. +static int falcon_i2c_tx(struct falcon_i2c *priv, int ten_bit, u16 addr,
  328. + u8 *buf, int len)
  329. +{
  330. + int i;
  331. + int ret;
  332. +
  333. + dev_dbg(priv->dev, "%s\n", __func__);
  334. +
  335. + /* tell fifo the number of bytes we are going to send */
  336. + i2c_w32(len + (ten_bit ? 2 : 1), tps_ctrl);
  337. +
  338. + /* wait for the data request */
  339. + ret = falcon_i2c_wait(priv, FALCON_I2C_BURST_REQ);
  340. + if (ret)
  341. + return ret;
  342. +
  343. + /* send slave address */
  344. + if (ten_bit) {
  345. + i2c_w32(0x78 | ((addr >> 7) & 0x7), txd);
  346. + i2c_w32(0x78 | ((addr & 0x7f) << 1) | 0, txd);
  347. + } else {
  348. + i2c_w32((addr << 1) | 0, txd);
  349. + }
  350. +
  351. + /* fill fifo */
  352. + for (i = 0; i < len; i++) {
  353. + ret = falcon_i2c_wait(priv, FALCON_I2C_BURST_REQ);
  354. + if (ret)
  355. + return ret;
  356. +
  357. + i2c_w32(buf[i], txd);
  358. + }
  359. +
  360. + return falcon_i2c_wait(priv, FALCON_I2C_TX_END);
  361. +}
  362. +
  363. +static int falcon_i2c_rx(struct falcon_i2c *priv, int ten_bit, u16 addr,
  364. + u8 *buf, int len)
  365. +{
  366. + int i;
  367. + int ret;
  368. +
  369. + dev_dbg(priv->dev, "%s\n", __func__);
  370. +
  371. + /* we need to transmit address only */
  372. + i2c_w32(ten_bit ? 2 : 1, tps_ctrl);
  373. +
  374. + /* set maximum received packet size */
  375. + i2c_w32(len, mrps_ctrl);
  376. +
  377. + /* wait for the data request */
  378. + ret = falcon_i2c_wait(priv, FALCON_I2C_BURST_REQ);
  379. + if (ret)
  380. + return ret;
  381. +
  382. + /* write down the address */
  383. + if (ten_bit) {
  384. + i2c_w32(0x78 | ((addr >> 7) & 0x7), txd);
  385. + i2c_w32(0x78 | ((addr & 0x7f) << 1) | 1, txd);
  386. + } else {
  387. + i2c_w32((addr << 1) | 1, txd);
  388. + }
  389. +
  390. + /* wait for the read request */
  391. + ret = falcon_i2c_wait(priv, FALCON_I2C_TX_END
  392. +#ifndef FALCON_FIX_ME
  393. + | FALCON_I2C_ADDR_MATCH
  394. +#endif
  395. + | FALCON_I2C_RX);
  396. +
  397. + if (ret)
  398. + return ret;
  399. +
  400. + /* read bytes */
  401. + for (i = 0; i < len; i++) {
  402. +#ifdef FALCON_FIX_ME
  403. + while (i2c_r32(rps_stat) == 0)
  404. + cpu_relax();
  405. +#else
  406. + ret = falcon_i2c_wait(priv, FALCON_I2C_BURST_REQ);
  407. +
  408. + if (ret)
  409. + return ret;
  410. +#endif
  411. +
  412. + buf[i] = i2c_r32(rxd);
  413. + }
  414. +
  415. +#ifndef FALCON_FIX_ME
  416. + /* wait for transmission end */
  417. + return falcon_i2c_wait(priv, FALCON_I2C_TX_END);
  418. +#else
  419. + return 0;
  420. +#endif
  421. +}
  422. +
  423. +static int falcon_i2c_xfer_msg(struct falcon_i2c *priv, struct i2c_msg *msg)
  424. +{
  425. + int ret;
  426. + int ten_bit;
  427. + unsigned long flags;
  428. +
  429. + dev_dbg(priv->dev, "%s %u byte(s) %s 0x%02x\n",
  430. + (msg->flags & I2C_M_RD) ? "read" : "write", msg->len,
  431. + (msg->flags & I2C_M_RD) ? "from" : "to", msg->addr);
  432. +
  433. + if (msg->flags & I2C_M_TEN)
  434. + ten_bit = 1;
  435. + else
  436. + ten_bit = 0;
  437. +
  438. + /* reconfigure bus if need to send message in different address mode */
  439. + spin_lock_irqsave(&priv->lock, flags);
  440. + if (ten_bit != priv->ten_bit) {
  441. +
  442. + /* disable bus */
  443. + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
  444. +
  445. + /* reconfigure address */
  446. + falcon_addr_configure(priv, ten_bit);
  447. +
  448. + /* enable bus */
  449. + i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
  450. +
  451. + priv->ten_bit = ten_bit;
  452. + }
  453. + spin_unlock_irqrestore(&priv->lock, flags);
  454. +
  455. + /* read/write actual message */
  456. + if (msg->flags & I2C_M_RD)
  457. + ret = falcon_i2c_rx(priv, ten_bit, msg->addr, msg->buf,
  458. + msg->len);
  459. + else
  460. + ret = falcon_i2c_tx(priv, ten_bit, msg->addr, msg->buf,
  461. + msg->len);
  462. +
  463. + if (ret)
  464. + return ret;
  465. +
  466. + return 0;
  467. +}
  468. +
  469. +static int falcon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  470. + int num)
  471. +{
  472. + int i;
  473. + int ret;
  474. + unsigned long flags;
  475. + struct falcon_i2c *priv = i2c_get_adapdata(adap);
  476. +
  477. + dev_dbg(priv->dev, "xfer %u messages\n", num);
  478. +
  479. + /* transfer each message */
  480. + for (i = 0; i < num; i++) {
  481. +#ifdef FALCON_FIX_ME
  482. + /* disable bus */
  483. + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
  484. + /* enable bus */
  485. + i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
  486. +#endif
  487. +
  488. + /* clear bus status */
  489. + spin_lock_irqsave(&priv->lock, flags);
  490. + priv->status = 0;
  491. + spin_unlock_irqrestore(&priv->lock, flags);
  492. +
  493. + /* wait for the bus to become ready */
  494. + ret = falcon_i2c_ready(priv);
  495. + if (ret)
  496. + return ret;
  497. +
  498. + /* transfer message */
  499. + ret = falcon_i2c_xfer_msg(priv, &msg[i]);
  500. +
  501. + if (ret)
  502. + return ret;
  503. +
  504. + /* check for unhandled errors */
  505. + spin_lock_irqsave(&priv->lock, flags);
  506. + if (FALCON_I2C_ERROR(priv))
  507. + ret = priv->status;
  508. + spin_unlock_irqrestore(&priv->lock, flags);
  509. +
  510. + if (ret) {
  511. + dev_warn(priv->dev, "message %u unhandled error 0x%x\n",
  512. + i, ret);
  513. +
  514. + return ret;
  515. + }
  516. + }
  517. +
  518. + return 0;
  519. +}
  520. +
  521. +static u32 falcon_i2c_func(struct i2c_adapter *adap)
  522. +{
  523. + return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
  524. +}
  525. +
  526. +static struct i2c_algorithm falcon_i2c_algorithm = {
  527. + .master_xfer = falcon_i2c_xfer,
  528. + .functionality = falcon_i2c_func,
  529. +};
  530. +
  531. +static int falcon_i2c_hw_init(struct i2c_adapter *adap)
  532. +{
  533. + struct falcon_i2c *priv = i2c_get_adapdata(adap);
  534. +
  535. + /* disable bus */
  536. + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
  537. +
  538. + /* set normal operation clock divider */
  539. + i2c_w32(1 << I2C_CLC_RMC_OFFSET, clc);
  540. +
  541. + /* set frequency */
  542. + if (priv->mode == FALCON_I2C_MODE_100) {
  543. + dev_dbg(priv->dev, "set standard mode (100 kHz)\n");
  544. +
  545. + i2c_w32(0, fdiv_high_cfg);
  546. +
  547. + i2c_w32((1 << I2C_FDIV_CFG_INC_OFFSET)
  548. + | (499 << I2C_FDIV_CFG_DEC_OFFSET),
  549. + fdiv_cfg);
  550. + } else if (priv->mode == FALCON_I2C_MODE_400) {
  551. + dev_dbg(priv->dev, "set fast mode (400 kHz)\n");
  552. +
  553. + i2c_w32(0, fdiv_high_cfg);
  554. +
  555. + i2c_w32((1 << I2C_FDIV_CFG_INC_OFFSET)
  556. + | (124 << I2C_FDIV_CFG_DEC_OFFSET),
  557. + fdiv_cfg);
  558. + } else if (priv->mode == FALCON_I2C_MODE_3400) {
  559. + dev_dbg(priv->dev, "set high mode (3.4 MHz)\n");
  560. +
  561. + i2c_w32(0, fdiv_cfg);
  562. +
  563. + /* TODO recalculate value for 100MHz input */
  564. + i2c_w32((41 << I2C_FDIV_CFG_INC_OFFSET)
  565. + | (152 << I2C_FDIV_CFG_DEC_OFFSET),
  566. + fdiv_high_cfg);
  567. + } else {
  568. + dev_warn(priv->dev, "unknown mode\n");
  569. +
  570. + return -ENODEV;
  571. + }
  572. +
  573. + /* configure fifo */
  574. + i2c_w32(I2C_FIFO_CFG_TXFC /* tx fifo as flow controller */
  575. + | I2C_FIFO_CFG_RXFC /* rx fifo as flow controller */
  576. + | I2C_FIFO_CFG_TXFA_TXFA2 /* tx fifo 4-byte aligned */
  577. + | I2C_FIFO_CFG_RXFA_RXFA2 /* rx fifo 4-byte aligned */
  578. + | I2C_FIFO_CFG_TXBS_TXBS0 /* tx fifo burst size is 1 word */
  579. + | I2C_FIFO_CFG_RXBS_RXBS0, /* rx fifo burst size is 1 word */
  580. + fifo_cfg);
  581. +
  582. + /* configure address */
  583. + falcon_addr_configure(priv, priv->ten_bit);
  584. +
  585. + /* enable bus */
  586. + i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
  587. +
  588. + /* mask burst interrupts */
  589. + i2c_w32_mask(I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, 0, imsc);
  590. +
  591. + /* enable interrupts */
  592. + i2c_w32(I2C_IMSC_LBREQ_INT_EN
  593. + | I2C_IMSC_BREQ_INT_EN
  594. + | I2C_IMSC_I2C_P_INT_EN
  595. + | I2C_IMSC_I2C_ERR_INT_EN,
  596. + imsc);
  597. +
  598. + return 0;
  599. +}
  600. +
  601. +static int __devinit falcon_i2c_probe(struct platform_device *pdev)
  602. +{
  603. + int ret = 0;
  604. + struct falcon_i2c *priv;
  605. + struct i2c_adapter *adap;
  606. + struct resource *mmres, *ioarea,
  607. + *irqres_lb, *irqres_b, *irqres_err, *irqres_p;
  608. + struct clk *clk;
  609. +
  610. + dev_dbg(&pdev->dev, "probing\n");
  611. +
  612. + mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  613. + irqres_lb = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  614. + "i2c_lb");
  615. + irqres_b = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "i2c_b");
  616. + irqres_err = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  617. + "i2c_err");
  618. + irqres_p = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "i2c_p");
  619. +
  620. + if (!mmres || !irqres_lb || !irqres_b || !irqres_err || !irqres_p) {
  621. + dev_err(&pdev->dev, "no resources\n");
  622. + return -ENODEV;
  623. + }
  624. +
  625. + clk = clk_get(&pdev->dev, "fpi");
  626. + if (IS_ERR(clk)) {
  627. + dev_err(&pdev->dev, "failed to get fpi clk\n");
  628. + return -ENOENT;
  629. + }
  630. +
  631. + if (clk_get_rate(clk) != 100000000) {
  632. + dev_err(&pdev->dev, "input clock is not 100MHz\n");
  633. + return -ENOENT;
  634. + }
  635. +
  636. + /* allocate private data */
  637. + priv = kzalloc(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 = &falcon_i2c_algorithm;
  649. +
  650. + priv->ten_bit = 0;
  651. + priv->mode = FALCON_I2C_MODE_100;
  652. + priv->clk = clk;
  653. + priv->dev = &pdev->dev;
  654. +
  655. + spin_lock_init(&priv->lock);
  656. +
  657. + ioarea = request_mem_region(mmres->start, resource_size(mmres),
  658. + pdev->name);
  659. +
  660. + if (ioarea == NULL) {
  661. + dev_err(&pdev->dev, "I2C region already claimed\n");
  662. + ret = -ENXIO;
  663. + goto err_free_priv;
  664. + }
  665. +
  666. + /* map memory */
  667. + priv->membase = ioremap_nocache(mmres->start & ~KSEG1,
  668. + resource_size(mmres));
  669. + if (priv->membase == NULL) {
  670. + ret = -ENOMEM;
  671. + goto err_release_region;
  672. + }
  673. +
  674. + priv->irq_lb = irqres_lb->start;
  675. + ret = request_irq(priv->irq_lb, falcon_i2c_isr, IRQF_DISABLED,
  676. + irqres_lb->name, priv);
  677. + if (ret) {
  678. + dev_err(&pdev->dev, "can't get last burst IRQ %d\n", irqres_lb->start);
  679. + ret = -ENODEV;
  680. + goto err_unmap_mem;
  681. + }
  682. +
  683. + priv->irq_b = irqres_b->start;
  684. + ret = request_irq(priv->irq_b, falcon_i2c_isr, IRQF_DISABLED,
  685. + irqres_b->name, priv);
  686. + if (ret) {
  687. + dev_err(&pdev->dev, "can't get burst IRQ %d\n", irqres_b->start);
  688. + ret = -ENODEV;
  689. + goto err_free_lb_irq;
  690. + }
  691. +
  692. + priv->irq_err = irqres_err->start;
  693. + ret = request_irq(priv->irq_err, falcon_i2c_isr, IRQF_DISABLED,
  694. + irqres_err->name, priv);
  695. + if (ret) {
  696. + dev_err(&pdev->dev, "can't get error IRQ %d\n", irqres_err->start);
  697. + ret = -ENODEV;
  698. + goto err_free_b_irq;
  699. + }
  700. +
  701. + priv->irq_p = irqres_p->start;
  702. + ret = request_irq(priv->irq_p, falcon_i2c_isr, IRQF_DISABLED,
  703. + irqres_p->name, priv);
  704. + if (ret) {
  705. + dev_err(&pdev->dev, "can't get protocol IRQ %d\n", irqres_p->start);
  706. + ret = -ENODEV;
  707. + goto err_free_err_irq;
  708. + }
  709. +
  710. + dev_dbg(&pdev->dev, "mapped io-space to %p\n", priv->membase);
  711. + dev_dbg(&pdev->dev, "use IRQs %d, %d, %d, %d\n", irqres_lb->start,
  712. + irqres_b->start, irqres_err->start, irqres_p->start);
  713. +
  714. + /* add our adapter to the i2c stack */
  715. + ret = i2c_add_numbered_adapter(adap);
  716. + if (ret) {
  717. + dev_err(&pdev->dev, "can't register I2C adapter\n");
  718. + goto err_free_p_irq;
  719. + }
  720. +
  721. + platform_set_drvdata(pdev, priv);
  722. + i2c_set_adapdata(adap, priv);
  723. +
  724. + /* print module version information */
  725. + dev_dbg(&pdev->dev, "module id=%u revision=%u\n",
  726. + (i2c_r32(id) & I2C_ID_ID_MASK) >> I2C_ID_ID_OFFSET,
  727. + (i2c_r32(id) & I2C_ID_REV_MASK) >> I2C_ID_REV_OFFSET);
  728. +
  729. + init_completion(&priv->done);
  730. +
  731. + /* initialize HW */
  732. + ret = falcon_i2c_hw_init(adap);
  733. + if (ret) {
  734. + dev_err(&pdev->dev, "can't configure adapter\n");
  735. + goto err_remove_adapter;
  736. + }
  737. +
  738. + return 0;
  739. +
  740. +err_remove_adapter:
  741. + i2c_del_adapter(adap);
  742. + platform_set_drvdata(pdev, NULL);
  743. +
  744. +err_free_p_irq:
  745. + free_irq(priv->irq_p, priv);
  746. +
  747. +err_free_err_irq:
  748. + free_irq(priv->irq_err, priv);
  749. +
  750. +err_free_b_irq:
  751. + free_irq(priv->irq_b, priv);
  752. +
  753. +err_free_lb_irq:
  754. + free_irq(priv->irq_lb, priv);
  755. +
  756. +err_unmap_mem:
  757. + iounmap(priv->membase);
  758. +
  759. +err_release_region:
  760. + release_mem_region(mmres->start, resource_size(mmres));
  761. +
  762. +err_free_priv:
  763. + kfree(priv);
  764. +
  765. + return ret;
  766. +}
  767. +
  768. +static int __devexit falcon_i2c_remove(struct platform_device *pdev)
  769. +{
  770. + struct falcon_i2c *priv = platform_get_drvdata(pdev);
  771. + struct resource *mmres;
  772. +
  773. + /* disable bus */
  774. + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
  775. +
  776. + /* remove driver */
  777. + platform_set_drvdata(pdev, NULL);
  778. + i2c_del_adapter(&priv->adap);
  779. +
  780. + free_irq(priv->irq_lb, priv);
  781. + free_irq(priv->irq_b, priv);
  782. + free_irq(priv->irq_err, priv);
  783. + free_irq(priv->irq_p, priv);
  784. +
  785. + kfree(priv);
  786. +
  787. + mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  788. + release_mem_region(mmres->start, resource_size(mmres));
  789. +
  790. + dev_dbg(&pdev->dev, "removed\n");
  791. +
  792. + return 0;
  793. +}
  794. +
  795. +static struct platform_driver falcon_i2c_driver = {
  796. + .probe = falcon_i2c_probe,
  797. + .remove = __devexit_p(falcon_i2c_remove),
  798. + .driver = {
  799. + .name = DRV_NAME,
  800. + .owner = THIS_MODULE,
  801. + },
  802. +};
  803. +
  804. +static int __init falcon_i2c_init(void)
  805. +{
  806. + int ret;
  807. +
  808. + ret = platform_driver_register(&falcon_i2c_driver);
  809. +
  810. + if (ret)
  811. + printk(KERN_DEBUG DRV_NAME ": can't register platform driver");
  812. +
  813. + return ret;
  814. +}
  815. +
  816. +static void __exit falcon_i2c_exit(void)
  817. +{
  818. + platform_driver_unregister(&falcon_i2c_driver);
  819. +}
  820. +
  821. +module_init(falcon_i2c_init);
  822. +module_exit(falcon_i2c_exit);
  823. +
  824. +MODULE_DESCRIPTION("Lantiq FALC(tm) ON - I2C bus adapter");
  825. +MODULE_ALIAS("platform:i2c_falcon");
  826. +MODULE_LICENSE("GPL");