i2c-cpm.c 17 KB

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