i2c-cpm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Freescale CPM1/CPM2 I2C interface.
  3. * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
  4. *
  5. * moved into proper i2c interface;
  6. * Brad Parker (brad@heeltoe.com)
  7. *
  8. * Parts from dbox2_i2c.c (cvs.tuxbox.org)
  9. * (C) 2000-2001 Felix Domke (tmbinc@gmx.net), Gillem (htoa@gmx.net)
  10. *
  11. * (C) 2007 Montavista Software, Inc.
  12. * Vitaly Bordug <vitb@kernel.crashing.org>
  13. *
  14. * Converted to of_platform_device. Renamed to i2c-cpm.c.
  15. * (C) 2007,2008 Jochen Friedrich <jochen@scram.de>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/delay.h>
  34. #include <linux/slab.h>
  35. #include <linux/init.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/errno.h>
  38. #include <linux/stddef.h>
  39. #include <linux/i2c.h>
  40. #include <linux/io.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/of_device.h>
  43. #include <linux/of_platform.h>
  44. #include <linux/of_i2c.h>
  45. #include <sysdev/fsl_soc.h>
  46. #include <asm/cpm.h>
  47. /* Try to define this if you have an older CPU (earlier than rev D4) */
  48. /* However, better use a GPIO based bitbang driver in this case :/ */
  49. #undef I2C_CHIP_ERRATA
  50. #define CPM_MAX_READ 513
  51. #define CPM_MAXBD 4
  52. #define I2C_EB (0x10) /* Big endian mode */
  53. #define I2C_EB_CPM2 (0x30) /* Big endian mode, memory snoop */
  54. #define DPRAM_BASE ((u8 __iomem __force *)cpm_muram_addr(0))
  55. /* I2C parameter RAM. */
  56. struct i2c_ram {
  57. ushort rbase; /* Rx Buffer descriptor base address */
  58. ushort tbase; /* Tx Buffer descriptor base address */
  59. u_char rfcr; /* Rx function code */
  60. u_char tfcr; /* Tx function code */
  61. ushort mrblr; /* Max receive buffer length */
  62. uint rstate; /* Internal */
  63. uint rdp; /* Internal */
  64. ushort rbptr; /* Rx Buffer descriptor pointer */
  65. ushort rbc; /* Internal */
  66. uint rxtmp; /* Internal */
  67. uint tstate; /* Internal */
  68. uint tdp; /* Internal */
  69. ushort tbptr; /* Tx Buffer descriptor pointer */
  70. ushort tbc; /* Internal */
  71. uint txtmp; /* Internal */
  72. char res1[4]; /* Reserved */
  73. ushort rpbase; /* Relocation pointer */
  74. char res2[2]; /* Reserved */
  75. };
  76. #define I2COM_START 0x80
  77. #define I2COM_MASTER 0x01
  78. #define I2CER_TXE 0x10
  79. #define I2CER_BUSY 0x04
  80. #define I2CER_TXB 0x02
  81. #define I2CER_RXB 0x01
  82. #define I2MOD_EN 0x01
  83. /* I2C Registers */
  84. struct i2c_reg {
  85. u8 i2mod;
  86. u8 res1[3];
  87. u8 i2add;
  88. u8 res2[3];
  89. u8 i2brg;
  90. u8 res3[3];
  91. u8 i2com;
  92. u8 res4[3];
  93. u8 i2cer;
  94. u8 res5[3];
  95. u8 i2cmr;
  96. };
  97. struct cpm_i2c {
  98. char *base;
  99. struct of_device *ofdev;
  100. struct i2c_adapter adap;
  101. uint dp_addr;
  102. int version; /* CPM1=1, CPM2=2 */
  103. int irq;
  104. int cp_command;
  105. int freq;
  106. struct i2c_reg __iomem *i2c_reg;
  107. struct i2c_ram __iomem *i2c_ram;
  108. u16 i2c_addr;
  109. wait_queue_head_t i2c_wait;
  110. cbd_t __iomem *tbase;
  111. cbd_t __iomem *rbase;
  112. u_char *txbuf[CPM_MAXBD];
  113. u_char *rxbuf[CPM_MAXBD];
  114. u32 txdma[CPM_MAXBD];
  115. u32 rxdma[CPM_MAXBD];
  116. };
  117. static irqreturn_t cpm_i2c_interrupt(int irq, void *dev_id)
  118. {
  119. struct cpm_i2c *cpm;
  120. struct i2c_reg __iomem *i2c_reg;
  121. struct i2c_adapter *adap = dev_id;
  122. int i;
  123. cpm = i2c_get_adapdata(dev_id);
  124. i2c_reg = cpm->i2c_reg;
  125. /* Clear interrupt. */
  126. i = in_8(&i2c_reg->i2cer);
  127. out_8(&i2c_reg->i2cer, i);
  128. dev_dbg(&adap->dev, "Interrupt: %x\n", i);
  129. wake_up_interruptible(&cpm->i2c_wait);
  130. return i ? IRQ_HANDLED : IRQ_NONE;
  131. }
  132. static void cpm_reset_i2c_params(struct cpm_i2c *cpm)
  133. {
  134. struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
  135. /* Set up the I2C parameters in the parameter ram. */
  136. out_be16(&i2c_ram->tbase, (u8 __iomem *)cpm->tbase - DPRAM_BASE);
  137. out_be16(&i2c_ram->rbase, (u8 __iomem *)cpm->rbase - DPRAM_BASE);
  138. if (cpm->version == 1) {
  139. out_8(&i2c_ram->tfcr, I2C_EB);
  140. out_8(&i2c_ram->rfcr, I2C_EB);
  141. } else {
  142. out_8(&i2c_ram->tfcr, I2C_EB_CPM2);
  143. out_8(&i2c_ram->rfcr, I2C_EB_CPM2);
  144. }
  145. out_be16(&i2c_ram->mrblr, CPM_MAX_READ);
  146. out_be32(&i2c_ram->rstate, 0);
  147. out_be32(&i2c_ram->rdp, 0);
  148. out_be16(&i2c_ram->rbptr, 0);
  149. out_be16(&i2c_ram->rbc, 0);
  150. out_be32(&i2c_ram->rxtmp, 0);
  151. out_be32(&i2c_ram->tstate, 0);
  152. out_be32(&i2c_ram->tdp, 0);
  153. out_be16(&i2c_ram->tbptr, 0);
  154. out_be16(&i2c_ram->tbc, 0);
  155. out_be32(&i2c_ram->txtmp, 0);
  156. }
  157. static void cpm_i2c_force_close(struct i2c_adapter *adap)
  158. {
  159. struct cpm_i2c *cpm = i2c_get_adapdata(adap);
  160. struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
  161. dev_dbg(&adap->dev, "cpm_i2c_force_close()\n");
  162. cpm_command(cpm->cp_command, CPM_CR_CLOSE_RX_BD);
  163. out_8(&i2c_reg->i2cmr, 0x00); /* Disable all interrupts */
  164. out_8(&i2c_reg->i2cer, 0xff);
  165. }
  166. static void cpm_i2c_parse_message(struct i2c_adapter *adap,
  167. struct i2c_msg *pmsg, int num, int tx, int rx)
  168. {
  169. cbd_t __iomem *tbdf;
  170. cbd_t __iomem *rbdf;
  171. u_char addr;
  172. u_char *tb;
  173. u_char *rb;
  174. struct cpm_i2c *cpm = i2c_get_adapdata(adap);
  175. tbdf = cpm->tbase + tx;
  176. rbdf = cpm->rbase + rx;
  177. addr = pmsg->addr << 1;
  178. if (pmsg->flags & I2C_M_RD)
  179. addr |= 1;
  180. tb = cpm->txbuf[tx];
  181. rb = cpm->rxbuf[rx];
  182. /* Align read buffer */
  183. rb = (u_char *) (((ulong) rb + 1) & ~1);
  184. tb[0] = addr; /* Device address byte w/rw flag */
  185. out_be16(&tbdf->cbd_datlen, pmsg->len + 1);
  186. out_be16(&tbdf->cbd_sc, 0);
  187. if (!(pmsg->flags & I2C_M_NOSTART))
  188. setbits16(&tbdf->cbd_sc, BD_I2C_START);
  189. if (tx + 1 == num)
  190. setbits16(&tbdf->cbd_sc, BD_SC_LAST | BD_SC_WRAP);
  191. if (pmsg->flags & I2C_M_RD) {
  192. /*
  193. * To read, we need an empty buffer of the proper length.
  194. * All that is used is the first byte for address, the remainder
  195. * is just used for timing (and doesn't really have to exist).
  196. */
  197. dev_dbg(&adap->dev, "cpm_i2c_read(abyte=0x%x)\n", addr);
  198. out_be16(&rbdf->cbd_datlen, 0);
  199. out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
  200. if (rx + 1 == CPM_MAXBD)
  201. setbits16(&rbdf->cbd_sc, BD_SC_WRAP);
  202. eieio();
  203. setbits16(&tbdf->cbd_sc, BD_SC_READY);
  204. } else {
  205. dev_dbg(&adap->dev, "cpm_i2c_write(abyte=0x%x)\n", addr);
  206. memcpy(tb+1, pmsg->buf, pmsg->len);
  207. eieio();
  208. setbits16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_INTRPT);
  209. }
  210. }
  211. static int cpm_i2c_check_message(struct i2c_adapter *adap,
  212. struct i2c_msg *pmsg, int tx, int rx)
  213. {
  214. cbd_t __iomem *tbdf;
  215. cbd_t __iomem *rbdf;
  216. u_char *tb;
  217. u_char *rb;
  218. struct cpm_i2c *cpm = i2c_get_adapdata(adap);
  219. tbdf = cpm->tbase + tx;
  220. rbdf = cpm->rbase + rx;
  221. tb = cpm->txbuf[tx];
  222. rb = cpm->rxbuf[rx];
  223. /* Align read buffer */
  224. rb = (u_char *) (((uint) rb + 1) & ~1);
  225. eieio();
  226. if (pmsg->flags & I2C_M_RD) {
  227. dev_dbg(&adap->dev, "tx sc 0x%04x, rx sc 0x%04x\n",
  228. in_be16(&tbdf->cbd_sc), in_be16(&rbdf->cbd_sc));
  229. if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
  230. dev_dbg(&adap->dev, "I2C read; No ack\n");
  231. return -ENXIO;
  232. }
  233. if (in_be16(&rbdf->cbd_sc) & BD_SC_EMPTY) {
  234. dev_err(&adap->dev,
  235. "I2C read; complete but rbuf empty\n");
  236. return -EREMOTEIO;
  237. }
  238. if (in_be16(&rbdf->cbd_sc) & BD_SC_OV) {
  239. dev_err(&adap->dev, "I2C read; Overrun\n");
  240. return -EREMOTEIO;
  241. }
  242. memcpy(pmsg->buf, rb, pmsg->len);
  243. } else {
  244. dev_dbg(&adap->dev, "tx sc %d 0x%04x\n", tx,
  245. in_be16(&tbdf->cbd_sc));
  246. if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
  247. dev_dbg(&adap->dev, "I2C write; No ack\n");
  248. return -ENXIO;
  249. }
  250. if (in_be16(&tbdf->cbd_sc) & BD_SC_UN) {
  251. dev_err(&adap->dev, "I2C write; Underrun\n");
  252. return -EIO;
  253. }
  254. if (in_be16(&tbdf->cbd_sc) & BD_SC_CL) {
  255. dev_err(&adap->dev, "I2C write; Collision\n");
  256. return -EIO;
  257. }
  258. }
  259. return 0;
  260. }
  261. static int cpm_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  262. {
  263. struct cpm_i2c *cpm = i2c_get_adapdata(adap);
  264. struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
  265. struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
  266. struct i2c_msg *pmsg;
  267. int ret, i;
  268. int tptr;
  269. int rptr;
  270. cbd_t __iomem *tbdf;
  271. cbd_t __iomem *rbdf;
  272. if (num > CPM_MAXBD)
  273. return -EINVAL;
  274. /* Check if we have any oversized READ requests */
  275. for (i = 0; i < num; i++) {
  276. pmsg = &msgs[i];
  277. if (pmsg->len >= CPM_MAX_READ)
  278. return -EINVAL;
  279. }
  280. /* Reset to use first buffer */
  281. out_be16(&i2c_ram->rbptr, in_be16(&i2c_ram->rbase));
  282. out_be16(&i2c_ram->tbptr, in_be16(&i2c_ram->tbase));
  283. tbdf = cpm->tbase;
  284. rbdf = cpm->rbase;
  285. tptr = 0;
  286. rptr = 0;
  287. while (tptr < num) {
  288. pmsg = &msgs[tptr];
  289. dev_dbg(&adap->dev, "R: %d T: %d\n", rptr, tptr);
  290. cpm_i2c_parse_message(adap, pmsg, num, tptr, rptr);
  291. if (pmsg->flags & I2C_M_RD)
  292. rptr++;
  293. tptr++;
  294. }
  295. /* Start transfer now */
  296. /* Enable RX/TX/Error interupts */
  297. out_8(&i2c_reg->i2cmr, I2CER_TXE | I2CER_TXB | I2CER_RXB);
  298. out_8(&i2c_reg->i2cer, 0xff); /* Clear interrupt status */
  299. /* Chip bug, set enable here */
  300. setbits8(&i2c_reg->i2mod, I2MOD_EN); /* Enable */
  301. /* Begin transmission */
  302. setbits8(&i2c_reg->i2com, I2COM_START);
  303. tptr = 0;
  304. rptr = 0;
  305. while (tptr < num) {
  306. /* Check for outstanding messages */
  307. dev_dbg(&adap->dev, "test ready.\n");
  308. pmsg = &msgs[tptr];
  309. if (pmsg->flags & I2C_M_RD)
  310. ret = wait_event_interruptible_timeout(cpm->i2c_wait,
  311. !(in_be16(&rbdf[rptr].cbd_sc) & BD_SC_EMPTY),
  312. 1 * HZ);
  313. else
  314. ret = wait_event_interruptible_timeout(cpm->i2c_wait,
  315. !(in_be16(&tbdf[tptr].cbd_sc) & BD_SC_READY),
  316. 1 * HZ);
  317. if (ret == 0) {
  318. ret = -EREMOTEIO;
  319. dev_err(&adap->dev, "I2C transfer: timeout\n");
  320. goto out_err;
  321. }
  322. if (ret > 0) {
  323. dev_dbg(&adap->dev, "ready.\n");
  324. ret = cpm_i2c_check_message(adap, pmsg, tptr, rptr);
  325. tptr++;
  326. if (pmsg->flags & I2C_M_RD)
  327. rptr++;
  328. if (ret)
  329. goto out_err;
  330. }
  331. }
  332. #ifdef I2C_CHIP_ERRATA
  333. /*
  334. * Chip errata, clear enable. This is not needed on rev D4 CPUs.
  335. * Disabling I2C too early may cause too short stop condition
  336. */
  337. udelay(4);
  338. clrbits8(&i2c_reg->i2mod, I2MOD_EN);
  339. #endif
  340. return (num);
  341. out_err:
  342. cpm_i2c_force_close(adap);
  343. #ifdef I2C_CHIP_ERRATA
  344. /*
  345. * Chip errata, clear enable. This is not needed on rev D4 CPUs.
  346. */
  347. clrbits8(&i2c_reg->i2mod, I2MOD_EN);
  348. #endif
  349. return ret;
  350. }
  351. static u32 cpm_i2c_func(struct i2c_adapter *adap)
  352. {
  353. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  354. }
  355. /* -----exported algorithm data: ------------------------------------- */
  356. static const struct i2c_algorithm cpm_i2c_algo = {
  357. .master_xfer = cpm_i2c_xfer,
  358. .functionality = cpm_i2c_func,
  359. };
  360. static const struct i2c_adapter cpm_ops = {
  361. .owner = THIS_MODULE,
  362. .name = "i2c-cpm",
  363. .algo = &cpm_i2c_algo,
  364. };
  365. static int __devinit cpm_i2c_setup(struct cpm_i2c *cpm)
  366. {
  367. struct of_device *ofdev = cpm->ofdev;
  368. const u32 *data;
  369. int len, ret, i;
  370. void __iomem *i2c_base;
  371. cbd_t __iomem *tbdf;
  372. cbd_t __iomem *rbdf;
  373. unsigned char brg;
  374. dev_dbg(&cpm->ofdev->dev, "cpm_i2c_setup()\n");
  375. init_waitqueue_head(&cpm->i2c_wait);
  376. cpm->irq = of_irq_to_resource(ofdev->node, 0, NULL);
  377. if (cpm->irq == NO_IRQ)
  378. return -EINVAL;
  379. /* Install interrupt handler. */
  380. ret = request_irq(cpm->irq, cpm_i2c_interrupt, 0, "cpm_i2c",
  381. &cpm->adap);
  382. if (ret)
  383. return ret;
  384. /* I2C parameter RAM */
  385. i2c_base = of_iomap(ofdev->node, 1);
  386. if (i2c_base == NULL) {
  387. ret = -EINVAL;
  388. goto out_irq;
  389. }
  390. if (of_device_is_compatible(ofdev->node, "fsl,cpm1-i2c")) {
  391. /* Check for and use a microcode relocation patch. */
  392. cpm->i2c_ram = i2c_base;
  393. cpm->i2c_addr = in_be16(&cpm->i2c_ram->rpbase);
  394. /*
  395. * Maybe should use cpm_muram_alloc instead of hardcoding
  396. * this in micropatch.c
  397. */
  398. if (cpm->i2c_addr) {
  399. cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
  400. iounmap(i2c_base);
  401. }
  402. cpm->version = 1;
  403. } else if (of_device_is_compatible(ofdev->node, "fsl,cpm2-i2c")) {
  404. cpm->i2c_addr = cpm_muram_alloc(sizeof(struct i2c_ram), 64);
  405. cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
  406. out_be16(i2c_base, cpm->i2c_addr);
  407. iounmap(i2c_base);
  408. cpm->version = 2;
  409. } else {
  410. iounmap(i2c_base);
  411. ret = -EINVAL;
  412. goto out_irq;
  413. }
  414. /* I2C control/status registers */
  415. cpm->i2c_reg = of_iomap(ofdev->node, 0);
  416. if (cpm->i2c_reg == NULL) {
  417. ret = -EINVAL;
  418. goto out_ram;
  419. }
  420. data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
  421. if (!data || len != 4) {
  422. ret = -EINVAL;
  423. goto out_reg;
  424. }
  425. cpm->cp_command = *data;
  426. data = of_get_property(ofdev->node, "linux,i2c-class", &len);
  427. if (data && len == 4)
  428. cpm->adap.class = *data;
  429. data = of_get_property(ofdev->node, "clock-frequency", &len);
  430. if (data && len == 4)
  431. cpm->freq = *data;
  432. else
  433. cpm->freq = 60000; /* use 60kHz i2c clock by default */
  434. /*
  435. * Allocate space for CPM_MAXBD transmit and receive buffer
  436. * descriptors in the DP ram.
  437. */
  438. cpm->dp_addr = cpm_muram_alloc(sizeof(cbd_t) * 2 * CPM_MAXBD, 8);
  439. if (!cpm->dp_addr) {
  440. ret = -ENOMEM;
  441. goto out_reg;
  442. }
  443. cpm->tbase = cpm_muram_addr(cpm->dp_addr);
  444. cpm->rbase = cpm_muram_addr(cpm->dp_addr + sizeof(cbd_t) * CPM_MAXBD);
  445. /* Allocate TX and RX buffers */
  446. tbdf = cpm->tbase;
  447. rbdf = cpm->rbase;
  448. for (i = 0; i < CPM_MAXBD; i++) {
  449. cpm->rxbuf[i] = dma_alloc_coherent(
  450. NULL, CPM_MAX_READ + 1, &cpm->rxdma[i], GFP_KERNEL);
  451. if (!cpm->rxbuf[i]) {
  452. ret = -ENOMEM;
  453. goto out_muram;
  454. }
  455. out_be32(&rbdf[i].cbd_bufaddr, ((cpm->rxdma[i] + 1) & ~1));
  456. cpm->txbuf[i] = (unsigned char *)dma_alloc_coherent(
  457. NULL, CPM_MAX_READ + 1, &cpm->txdma[i], GFP_KERNEL);
  458. if (!cpm->txbuf[i]) {
  459. ret = -ENOMEM;
  460. goto out_muram;
  461. }
  462. out_be32(&tbdf[i].cbd_bufaddr, cpm->txdma[i]);
  463. }
  464. /* Initialize Tx/Rx parameters. */
  465. cpm_reset_i2c_params(cpm);
  466. dev_dbg(&cpm->ofdev->dev, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
  467. cpm->i2c_ram, cpm->i2c_addr, cpm->freq);
  468. dev_dbg(&cpm->ofdev->dev, "tbase 0x%04x, rbase 0x%04x\n",
  469. (u8 __iomem *)cpm->tbase - DPRAM_BASE,
  470. (u8 __iomem *)cpm->rbase - DPRAM_BASE);
  471. cpm_command(cpm->cp_command, CPM_CR_INIT_TRX);
  472. /*
  473. * Select an invalid address. Just make sure we don't use loopback mode
  474. */
  475. out_8(&cpm->i2c_reg->i2add, 0x7f << 1);
  476. /*
  477. * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
  478. * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
  479. * the actual i2c bus frequency.
  480. */
  481. brg = get_brgfreq() / (32 * 2 * cpm->freq) - 3;
  482. out_8(&cpm->i2c_reg->i2brg, brg);
  483. out_8(&cpm->i2c_reg->i2mod, 0x00);
  484. out_8(&cpm->i2c_reg->i2com, I2COM_MASTER); /* Master mode */
  485. /* Disable interrupts. */
  486. out_8(&cpm->i2c_reg->i2cmr, 0);
  487. out_8(&cpm->i2c_reg->i2cer, 0xff);
  488. return 0;
  489. out_muram:
  490. for (i = 0; i < CPM_MAXBD; i++) {
  491. if (cpm->rxbuf[i])
  492. dma_free_coherent(NULL, CPM_MAX_READ + 1,
  493. cpm->rxbuf[i], cpm->rxdma[i]);
  494. if (cpm->txbuf[i])
  495. dma_free_coherent(NULL, CPM_MAX_READ + 1,
  496. cpm->txbuf[i], cpm->txdma[i]);
  497. }
  498. cpm_muram_free(cpm->dp_addr);
  499. out_reg:
  500. iounmap(cpm->i2c_reg);
  501. out_ram:
  502. if ((cpm->version == 1) && (!cpm->i2c_addr))
  503. iounmap(cpm->i2c_ram);
  504. if (cpm->version == 2)
  505. cpm_muram_free(cpm->i2c_addr);
  506. out_irq:
  507. free_irq(cpm->irq, &cpm->adap);
  508. return ret;
  509. }
  510. static void cpm_i2c_shutdown(struct cpm_i2c *cpm)
  511. {
  512. int i;
  513. /* Shut down I2C. */
  514. clrbits8(&cpm->i2c_reg->i2mod, I2MOD_EN);
  515. /* Disable interrupts */
  516. out_8(&cpm->i2c_reg->i2cmr, 0);
  517. out_8(&cpm->i2c_reg->i2cer, 0xff);
  518. free_irq(cpm->irq, &cpm->adap);
  519. /* Free all memory */
  520. for (i = 0; i < CPM_MAXBD; i++) {
  521. dma_free_coherent(NULL, CPM_MAX_READ + 1,
  522. cpm->rxbuf[i], cpm->rxdma[i]);
  523. dma_free_coherent(NULL, CPM_MAX_READ + 1,
  524. cpm->txbuf[i], cpm->txdma[i]);
  525. }
  526. cpm_muram_free(cpm->dp_addr);
  527. iounmap(cpm->i2c_reg);
  528. if ((cpm->version == 1) && (!cpm->i2c_addr))
  529. iounmap(cpm->i2c_ram);
  530. if (cpm->version == 2)
  531. cpm_muram_free(cpm->i2c_addr);
  532. }
  533. static int __devinit cpm_i2c_probe(struct of_device *ofdev,
  534. const struct of_device_id *match)
  535. {
  536. int result, len;
  537. struct cpm_i2c *cpm;
  538. const u32 *data;
  539. cpm = kzalloc(sizeof(struct cpm_i2c), GFP_KERNEL);
  540. if (!cpm)
  541. return -ENOMEM;
  542. cpm->ofdev = ofdev;
  543. dev_set_drvdata(&ofdev->dev, cpm);
  544. cpm->adap = cpm_ops;
  545. i2c_set_adapdata(&cpm->adap, cpm);
  546. cpm->adap.dev.parent = &ofdev->dev;
  547. result = cpm_i2c_setup(cpm);
  548. if (result) {
  549. dev_err(&ofdev->dev, "Unable to init hardware\n");
  550. goto out_free;
  551. }
  552. /* register new adapter to i2c module... */
  553. data = of_get_property(ofdev->node, "linux,i2c-index", &len);
  554. if (data && len == 4) {
  555. cpm->adap.nr = *data;
  556. result = i2c_add_numbered_adapter(&cpm->adap);
  557. } else
  558. result = i2c_add_adapter(&cpm->adap);
  559. if (result < 0) {
  560. dev_err(&ofdev->dev, "Unable to register with I2C\n");
  561. goto out_shut;
  562. }
  563. dev_dbg(&ofdev->dev, "hw routines for %s registered.\n",
  564. cpm->adap.name);
  565. /*
  566. * register OF I2C devices
  567. */
  568. of_register_i2c_devices(&cpm->adap, ofdev->node);
  569. return 0;
  570. out_shut:
  571. cpm_i2c_shutdown(cpm);
  572. out_free:
  573. dev_set_drvdata(&ofdev->dev, NULL);
  574. kfree(cpm);
  575. return result;
  576. }
  577. static int __devexit cpm_i2c_remove(struct of_device *ofdev)
  578. {
  579. struct cpm_i2c *cpm = dev_get_drvdata(&ofdev->dev);
  580. i2c_del_adapter(&cpm->adap);
  581. cpm_i2c_shutdown(cpm);
  582. dev_set_drvdata(&ofdev->dev, NULL);
  583. kfree(cpm);
  584. return 0;
  585. }
  586. static const struct of_device_id cpm_i2c_match[] = {
  587. {
  588. .compatible = "fsl,cpm1-i2c",
  589. },
  590. {
  591. .compatible = "fsl,cpm2-i2c",
  592. },
  593. {},
  594. };
  595. MODULE_DEVICE_TABLE(of, cpm_i2c_match);
  596. static struct of_platform_driver cpm_i2c_driver = {
  597. .match_table = cpm_i2c_match,
  598. .probe = cpm_i2c_probe,
  599. .remove = __devexit_p(cpm_i2c_remove),
  600. .driver = {
  601. .name = "fsl-i2c-cpm",
  602. .owner = THIS_MODULE,
  603. }
  604. };
  605. static int __init cpm_i2c_init(void)
  606. {
  607. return of_register_platform_driver(&cpm_i2c_driver);
  608. }
  609. static void __exit cpm_i2c_exit(void)
  610. {
  611. of_unregister_platform_driver(&cpm_i2c_driver);
  612. }
  613. module_init(cpm_i2c_init);
  614. module_exit(cpm_i2c_exit);
  615. MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
  616. MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
  617. MODULE_LICENSE("GPL");