i2c-cpm.c 18 KB

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