i2c-cpm.c 17 KB

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