PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/i2c/busses/i2c-sh7760.c

https://bitbucket.org/Tofe/picasso-kernel
C | 567 lines | 431 code | 86 blank | 50 comment | 52 complexity | aa13f852db659039154d71732c3ecc7f MD5 | raw file
  1. /*
  2. * I2C bus driver for the SH7760 I2C Interfaces.
  3. *
  4. * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  5. *
  6. * licensed under the terms outlined in the file COPYING.
  7. *
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <asm/clock.h>
  21. #include <asm/i2c-sh7760.h>
  22. /* register offsets */
  23. #define I2CSCR 0x0 /* slave ctrl */
  24. #define I2CMCR 0x4 /* master ctrl */
  25. #define I2CSSR 0x8 /* slave status */
  26. #define I2CMSR 0xC /* master status */
  27. #define I2CSIER 0x10 /* slave irq enable */
  28. #define I2CMIER 0x14 /* master irq enable */
  29. #define I2CCCR 0x18 /* clock dividers */
  30. #define I2CSAR 0x1c /* slave address */
  31. #define I2CMAR 0x20 /* master address */
  32. #define I2CRXTX 0x24 /* data port */
  33. #define I2CFCR 0x28 /* fifo control */
  34. #define I2CFSR 0x2C /* fifo status */
  35. #define I2CFIER 0x30 /* fifo irq enable */
  36. #define I2CRFDR 0x34 /* rx fifo count */
  37. #define I2CTFDR 0x38 /* tx fifo count */
  38. #define REGSIZE 0x3C
  39. #define MCR_MDBS 0x80 /* non-fifo mode switch */
  40. #define MCR_FSCL 0x40 /* override SCL pin */
  41. #define MCR_FSDA 0x20 /* override SDA pin */
  42. #define MCR_OBPC 0x10 /* override pins */
  43. #define MCR_MIE 0x08 /* master if enable */
  44. #define MCR_TSBE 0x04
  45. #define MCR_FSB 0x02 /* force stop bit */
  46. #define MCR_ESG 0x01 /* en startbit gen. */
  47. #define MSR_MNR 0x40 /* nack received */
  48. #define MSR_MAL 0x20 /* arbitration lost */
  49. #define MSR_MST 0x10 /* sent a stop */
  50. #define MSR_MDE 0x08
  51. #define MSR_MDT 0x04
  52. #define MSR_MDR 0x02
  53. #define MSR_MAT 0x01 /* slave addr xfer done */
  54. #define MIE_MNRE 0x40 /* nack irq en */
  55. #define MIE_MALE 0x20 /* arblos irq en */
  56. #define MIE_MSTE 0x10 /* stop irq en */
  57. #define MIE_MDEE 0x08
  58. #define MIE_MDTE 0x04
  59. #define MIE_MDRE 0x02
  60. #define MIE_MATE 0x01 /* address sent irq en */
  61. #define FCR_RFRST 0x02 /* reset rx fifo */
  62. #define FCR_TFRST 0x01 /* reset tx fifo */
  63. #define FSR_TEND 0x04 /* last byte sent */
  64. #define FSR_RDF 0x02 /* rx fifo trigger */
  65. #define FSR_TDFE 0x01 /* tx fifo empty */
  66. #define FIER_TEIE 0x04 /* tx fifo empty irq en */
  67. #define FIER_RXIE 0x02 /* rx fifo trig irq en */
  68. #define FIER_TXIE 0x01 /* tx fifo trig irq en */
  69. #define FIFO_SIZE 16
  70. struct cami2c {
  71. void __iomem *iobase;
  72. struct i2c_adapter adap;
  73. /* message processing */
  74. struct i2c_msg *msg;
  75. #define IDF_SEND 1
  76. #define IDF_RECV 2
  77. #define IDF_STOP 4
  78. int flags;
  79. #define IDS_DONE 1
  80. #define IDS_ARBLOST 2
  81. #define IDS_NACK 4
  82. int status;
  83. struct completion xfer_done;
  84. int irq;
  85. struct resource *ioarea;
  86. };
  87. static inline void OUT32(struct cami2c *cam, int reg, unsigned long val)
  88. {
  89. __raw_writel(val, (unsigned long)cam->iobase + reg);
  90. }
  91. static inline unsigned long IN32(struct cami2c *cam, int reg)
  92. {
  93. return __raw_readl((unsigned long)cam->iobase + reg);
  94. }
  95. static irqreturn_t sh7760_i2c_irq(int irq, void *ptr)
  96. {
  97. struct cami2c *id = ptr;
  98. struct i2c_msg *msg = id->msg;
  99. char *data = msg->buf;
  100. unsigned long msr, fsr, fier, len;
  101. msr = IN32(id, I2CMSR);
  102. fsr = IN32(id, I2CFSR);
  103. /* arbitration lost */
  104. if (msr & MSR_MAL) {
  105. OUT32(id, I2CMCR, 0);
  106. OUT32(id, I2CSCR, 0);
  107. OUT32(id, I2CSAR, 0);
  108. id->status |= IDS_DONE | IDS_ARBLOST;
  109. goto out;
  110. }
  111. if (msr & MSR_MNR) {
  112. /* NACK handling is very screwed up. After receiving a
  113. * NAK IRQ one has to wait a bit before writing to any
  114. * registers, or the ctl will lock up. After that delay
  115. * do a normal i2c stop. Then wait at least 1 ms before
  116. * attempting another transfer or ctl will stop working
  117. */
  118. udelay(100); /* wait or risk ctl hang */
  119. OUT32(id, I2CFCR, FCR_RFRST | FCR_TFRST);
  120. OUT32(id, I2CMCR, MCR_MIE | MCR_FSB);
  121. OUT32(id, I2CFIER, 0);
  122. OUT32(id, I2CMIER, MIE_MSTE);
  123. OUT32(id, I2CSCR, 0);
  124. OUT32(id, I2CSAR, 0);
  125. id->status |= IDS_NACK;
  126. msr &= ~MSR_MAT;
  127. fsr = 0;
  128. /* In some cases the MST bit is also set. */
  129. }
  130. /* i2c-stop was sent */
  131. if (msr & MSR_MST) {
  132. id->status |= IDS_DONE;
  133. goto out;
  134. }
  135. /* i2c slave addr was sent; set to "normal" operation */
  136. if (msr & MSR_MAT)
  137. OUT32(id, I2CMCR, MCR_MIE);
  138. fier = IN32(id, I2CFIER);
  139. if (fsr & FSR_RDF) {
  140. len = IN32(id, I2CRFDR);
  141. if (msg->len <= len) {
  142. if (id->flags & IDF_STOP) {
  143. OUT32(id, I2CMCR, MCR_MIE | MCR_FSB);
  144. OUT32(id, I2CFIER, 0);
  145. /* manual says: wait >= 0.5 SCL times */
  146. udelay(5);
  147. /* next int should be MST */
  148. } else {
  149. id->status |= IDS_DONE;
  150. /* keep the RDF bit: ctrl holds SCL low
  151. * until the setup for the next i2c_msg
  152. * clears this bit.
  153. */
  154. fsr &= ~FSR_RDF;
  155. }
  156. }
  157. while (msg->len && len) {
  158. *data++ = IN32(id, I2CRXTX);
  159. msg->len--;
  160. len--;
  161. }
  162. if (msg->len) {
  163. len = (msg->len >= FIFO_SIZE) ? FIFO_SIZE - 1
  164. : msg->len - 1;
  165. OUT32(id, I2CFCR, FCR_TFRST | ((len & 0xf) << 4));
  166. }
  167. } else if (id->flags & IDF_SEND) {
  168. if ((fsr & FSR_TEND) && (msg->len < 1)) {
  169. if (id->flags & IDF_STOP) {
  170. OUT32(id, I2CMCR, MCR_MIE | MCR_FSB);
  171. } else {
  172. id->status |= IDS_DONE;
  173. /* keep the TEND bit: ctl holds SCL low
  174. * until the setup for the next i2c_msg
  175. * clears this bit.
  176. */
  177. fsr &= ~FSR_TEND;
  178. }
  179. }
  180. if (fsr & FSR_TDFE) {
  181. while (msg->len && (IN32(id, I2CTFDR) < FIFO_SIZE)) {
  182. OUT32(id, I2CRXTX, *data++);
  183. msg->len--;
  184. }
  185. if (msg->len < 1) {
  186. fier &= ~FIER_TXIE;
  187. OUT32(id, I2CFIER, fier);
  188. } else {
  189. len = (msg->len >= FIFO_SIZE) ? 2 : 0;
  190. OUT32(id, I2CFCR,
  191. FCR_RFRST | ((len & 3) << 2));
  192. }
  193. }
  194. }
  195. out:
  196. if (id->status & IDS_DONE) {
  197. OUT32(id, I2CMIER, 0);
  198. OUT32(id, I2CFIER, 0);
  199. id->msg = NULL;
  200. complete(&id->xfer_done);
  201. }
  202. /* clear status flags and ctrl resumes work */
  203. OUT32(id, I2CMSR, ~msr);
  204. OUT32(id, I2CFSR, ~fsr);
  205. OUT32(id, I2CSSR, 0);
  206. return IRQ_HANDLED;
  207. }
  208. /* prepare and start a master receive operation */
  209. static void sh7760_i2c_mrecv(struct cami2c *id)
  210. {
  211. int len;
  212. id->flags |= IDF_RECV;
  213. /* set the slave addr reg; otherwise rcv wont work! */
  214. OUT32(id, I2CSAR, 0xfe);
  215. OUT32(id, I2CMAR, (id->msg->addr << 1) | 1);
  216. /* adjust rx fifo trigger */
  217. if (id->msg->len >= FIFO_SIZE)
  218. len = FIFO_SIZE - 1; /* trigger at fifo full */
  219. else
  220. len = id->msg->len - 1; /* trigger before all received */
  221. OUT32(id, I2CFCR, FCR_RFRST | FCR_TFRST);
  222. OUT32(id, I2CFCR, FCR_TFRST | ((len & 0xF) << 4));
  223. OUT32(id, I2CMSR, 0);
  224. OUT32(id, I2CMCR, MCR_MIE | MCR_ESG);
  225. OUT32(id, I2CMIER, MIE_MNRE | MIE_MALE | MIE_MSTE | MIE_MATE);
  226. OUT32(id, I2CFIER, FIER_RXIE);
  227. }
  228. /* prepare and start a master send operation */
  229. static void sh7760_i2c_msend(struct cami2c *id)
  230. {
  231. int len;
  232. id->flags |= IDF_SEND;
  233. /* set the slave addr reg; otherwise xmit wont work! */
  234. OUT32(id, I2CSAR, 0xfe);
  235. OUT32(id, I2CMAR, (id->msg->addr << 1) | 0);
  236. /* adjust tx fifo trigger */
  237. if (id->msg->len >= FIFO_SIZE)
  238. len = 2; /* trig: 2 bytes left in TX fifo */
  239. else
  240. len = 0; /* trig: 8 bytes left in TX fifo */
  241. OUT32(id, I2CFCR, FCR_RFRST | FCR_TFRST);
  242. OUT32(id, I2CFCR, FCR_RFRST | ((len & 3) << 2));
  243. while (id->msg->len && IN32(id, I2CTFDR) < FIFO_SIZE) {
  244. OUT32(id, I2CRXTX, *(id->msg->buf));
  245. (id->msg->len)--;
  246. (id->msg->buf)++;
  247. }
  248. OUT32(id, I2CMSR, 0);
  249. OUT32(id, I2CMCR, MCR_MIE | MCR_ESG);
  250. OUT32(id, I2CFSR, 0);
  251. OUT32(id, I2CMIER, MIE_MNRE | MIE_MALE | MIE_MSTE | MIE_MATE);
  252. OUT32(id, I2CFIER, FIER_TEIE | (id->msg->len ? FIER_TXIE : 0));
  253. }
  254. static inline int sh7760_i2c_busy_check(struct cami2c *id)
  255. {
  256. return (IN32(id, I2CMCR) & MCR_FSDA);
  257. }
  258. static int sh7760_i2c_master_xfer(struct i2c_adapter *adap,
  259. struct i2c_msg *msgs,
  260. int num)
  261. {
  262. struct cami2c *id = adap->algo_data;
  263. int i, retr;
  264. if (sh7760_i2c_busy_check(id)) {
  265. dev_err(&adap->dev, "sh7760-i2c%d: bus busy!\n", adap->nr);
  266. return -EBUSY;
  267. }
  268. i = 0;
  269. while (i < num) {
  270. retr = adap->retries;
  271. retry:
  272. id->flags = ((i == (num-1)) ? IDF_STOP : 0);
  273. id->status = 0;
  274. id->msg = msgs;
  275. init_completion(&id->xfer_done);
  276. if (msgs->flags & I2C_M_RD)
  277. sh7760_i2c_mrecv(id);
  278. else
  279. sh7760_i2c_msend(id);
  280. wait_for_completion(&id->xfer_done);
  281. if (id->status == 0) {
  282. num = -EIO;
  283. break;
  284. }
  285. if (id->status & IDS_NACK) {
  286. /* wait a bit or i2c module stops working */
  287. mdelay(1);
  288. num = -EREMOTEIO;
  289. break;
  290. }
  291. if (id->status & IDS_ARBLOST) {
  292. if (retr--) {
  293. mdelay(2);
  294. goto retry;
  295. }
  296. num = -EREMOTEIO;
  297. break;
  298. }
  299. msgs++;
  300. i++;
  301. }
  302. id->msg = NULL;
  303. id->flags = 0;
  304. id->status = 0;
  305. OUT32(id, I2CMCR, 0);
  306. OUT32(id, I2CMSR, 0);
  307. OUT32(id, I2CMIER, 0);
  308. OUT32(id, I2CFIER, 0);
  309. /* reset slave module registers too: master mode enables slave
  310. * module for receive ops (ack, data). Without this reset,
  311. * eternal bus activity might be reported after NACK / ARBLOST.
  312. */
  313. OUT32(id, I2CSCR, 0);
  314. OUT32(id, I2CSAR, 0);
  315. OUT32(id, I2CSSR, 0);
  316. return num;
  317. }
  318. static u32 sh7760_i2c_func(struct i2c_adapter *adap)
  319. {
  320. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  321. }
  322. static const struct i2c_algorithm sh7760_i2c_algo = {
  323. .master_xfer = sh7760_i2c_master_xfer,
  324. .functionality = sh7760_i2c_func,
  325. };
  326. /* calculate CCR register setting for a desired scl clock. SCL clock is
  327. * derived from I2C module clock (iclk) which in turn is derived from
  328. * peripheral module clock (mclk, usually around 33MHz):
  329. * iclk = mclk/(CDF + 1). iclk must be < 20MHz.
  330. * scl = iclk/(SCGD*8 + 20).
  331. */
  332. static int calc_CCR(unsigned long scl_hz)
  333. {
  334. struct clk *mclk;
  335. unsigned long mck, m1, dff, odff, iclk;
  336. signed char cdf, cdfm;
  337. int scgd, scgdm, scgds;
  338. mclk = clk_get(NULL, "peripheral_clk");
  339. if (IS_ERR(mclk)) {
  340. return PTR_ERR(mclk);
  341. } else {
  342. mck = mclk->rate;
  343. clk_put(mclk);
  344. }
  345. odff = scl_hz;
  346. scgdm = cdfm = m1 = 0;
  347. for (cdf = 3; cdf >= 0; cdf--) {
  348. iclk = mck / (1 + cdf);
  349. if (iclk >= 20000000)
  350. continue;
  351. scgds = ((iclk / scl_hz) - 20) >> 3;
  352. for (scgd = scgds; (scgd < 63) && scgd <= scgds + 1; scgd++) {
  353. m1 = iclk / (20 + (scgd << 3));
  354. dff = abs(scl_hz - m1);
  355. if (dff < odff) {
  356. odff = dff;
  357. cdfm = cdf;
  358. scgdm = scgd;
  359. }
  360. }
  361. }
  362. /* fail if more than 25% off of requested SCL */
  363. if (odff > (scl_hz >> 2))
  364. return -EINVAL;
  365. /* create a CCR register value */
  366. return ((scgdm << 2) | cdfm);
  367. }
  368. static int sh7760_i2c_probe(struct platform_device *pdev)
  369. {
  370. struct sh7760_i2c_platdata *pd;
  371. struct resource *res;
  372. struct cami2c *id;
  373. int ret;
  374. pd = pdev->dev.platform_data;
  375. if (!pd) {
  376. dev_err(&pdev->dev, "no platform_data!\n");
  377. ret = -ENODEV;
  378. goto out0;
  379. }
  380. id = kzalloc(sizeof(struct cami2c), GFP_KERNEL);
  381. if (!id) {
  382. dev_err(&pdev->dev, "no mem for private data\n");
  383. ret = -ENOMEM;
  384. goto out0;
  385. }
  386. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  387. if (!res) {
  388. dev_err(&pdev->dev, "no mmio resources\n");
  389. ret = -ENODEV;
  390. goto out1;
  391. }
  392. id->ioarea = request_mem_region(res->start, REGSIZE, pdev->name);
  393. if (!id->ioarea) {
  394. dev_err(&pdev->dev, "mmio already reserved\n");
  395. ret = -EBUSY;
  396. goto out1;
  397. }
  398. id->iobase = ioremap(res->start, REGSIZE);
  399. if (!id->iobase) {
  400. dev_err(&pdev->dev, "cannot ioremap\n");
  401. ret = -ENODEV;
  402. goto out2;
  403. }
  404. id->irq = platform_get_irq(pdev, 0);
  405. id->adap.nr = pdev->id;
  406. id->adap.algo = &sh7760_i2c_algo;
  407. id->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  408. id->adap.retries = 3;
  409. id->adap.algo_data = id;
  410. id->adap.dev.parent = &pdev->dev;
  411. snprintf(id->adap.name, sizeof(id->adap.name),
  412. "SH7760 I2C at %08lx", (unsigned long)res->start);
  413. OUT32(id, I2CMCR, 0);
  414. OUT32(id, I2CMSR, 0);
  415. OUT32(id, I2CMIER, 0);
  416. OUT32(id, I2CMAR, 0);
  417. OUT32(id, I2CSIER, 0);
  418. OUT32(id, I2CSAR, 0);
  419. OUT32(id, I2CSCR, 0);
  420. OUT32(id, I2CSSR, 0);
  421. OUT32(id, I2CFIER, 0);
  422. OUT32(id, I2CFCR, FCR_RFRST | FCR_TFRST);
  423. OUT32(id, I2CFSR, 0);
  424. ret = calc_CCR(pd->speed_khz * 1000);
  425. if (ret < 0) {
  426. dev_err(&pdev->dev, "invalid SCL clock: %dkHz\n",
  427. pd->speed_khz);
  428. goto out3;
  429. }
  430. OUT32(id, I2CCCR, ret);
  431. if (request_irq(id->irq, sh7760_i2c_irq, 0,
  432. SH7760_I2C_DEVNAME, id)) {
  433. dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
  434. ret = -EBUSY;
  435. goto out3;
  436. }
  437. ret = i2c_add_numbered_adapter(&id->adap);
  438. if (ret < 0) {
  439. dev_err(&pdev->dev, "reg adap failed: %d\n", ret);
  440. goto out4;
  441. }
  442. platform_set_drvdata(pdev, id);
  443. dev_info(&pdev->dev, "%d kHz mmio %08x irq %d\n",
  444. pd->speed_khz, res->start, id->irq);
  445. return 0;
  446. out4:
  447. free_irq(id->irq, id);
  448. out3:
  449. iounmap(id->iobase);
  450. out2:
  451. release_resource(id->ioarea);
  452. kfree(id->ioarea);
  453. out1:
  454. kfree(id);
  455. out0:
  456. return ret;
  457. }
  458. static int sh7760_i2c_remove(struct platform_device *pdev)
  459. {
  460. struct cami2c *id = platform_get_drvdata(pdev);
  461. i2c_del_adapter(&id->adap);
  462. free_irq(id->irq, id);
  463. iounmap(id->iobase);
  464. release_resource(id->ioarea);
  465. kfree(id->ioarea);
  466. kfree(id);
  467. platform_set_drvdata(pdev, NULL);
  468. return 0;
  469. }
  470. static struct platform_driver sh7760_i2c_drv = {
  471. .driver = {
  472. .name = SH7760_I2C_DEVNAME,
  473. .owner = THIS_MODULE,
  474. },
  475. .probe = sh7760_i2c_probe,
  476. .remove = sh7760_i2c_remove,
  477. };
  478. module_platform_driver(sh7760_i2c_drv);
  479. MODULE_LICENSE("GPL");
  480. MODULE_DESCRIPTION("SH7760 I2C bus driver");
  481. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");