i2c-highlander.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Renesas Solutions Highlander FPGA I2C/SMBus support.
  3. *
  4. * Supported devices: R0P7780LC0011RL, R0P7785LC0011RL
  5. *
  6. * Copyright (C) 2008 Paul Mundt
  7. * Copyright (C) 2008 Renesas Solutions Corp.
  8. * Copyright (C) 2008 Atom Create Engineering Co., Ltd.
  9. *
  10. * This file is subject to the terms and conditions of the GNU General
  11. * Public License version 2. See the file "COPYING" in the main directory
  12. * of this archive for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/completion.h>
  20. #include <linux/io.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #define SMCR 0x00
  24. #define SMCR_START (1 << 0)
  25. #define SMCR_IRIC (1 << 1)
  26. #define SMCR_BBSY (1 << 2)
  27. #define SMCR_ACKE (1 << 3)
  28. #define SMCR_RST (1 << 4)
  29. #define SMCR_IEIC (1 << 6)
  30. #define SMSMADR 0x02
  31. #define SMMR 0x04
  32. #define SMMR_MODE0 (1 << 0)
  33. #define SMMR_MODE1 (1 << 1)
  34. #define SMMR_CAP (1 << 3)
  35. #define SMMR_TMMD (1 << 4)
  36. #define SMMR_SP (1 << 7)
  37. #define SMSADR 0x06
  38. #define SMTRDR 0x46
  39. struct highlander_i2c_dev {
  40. struct device *dev;
  41. void __iomem *base;
  42. struct i2c_adapter adapter;
  43. struct completion cmd_complete;
  44. unsigned long last_read_time;
  45. int irq;
  46. u8 *buf;
  47. size_t buf_len;
  48. };
  49. static int iic_force_poll, iic_force_normal;
  50. static int iic_timeout = 1000, iic_read_delay;
  51. static inline void highlander_i2c_irq_enable(struct highlander_i2c_dev *dev)
  52. {
  53. iowrite16(ioread16(dev->base + SMCR) | SMCR_IEIC, dev->base + SMCR);
  54. }
  55. static inline void highlander_i2c_irq_disable(struct highlander_i2c_dev *dev)
  56. {
  57. iowrite16(ioread16(dev->base + SMCR) & ~SMCR_IEIC, dev->base + SMCR);
  58. }
  59. static inline void highlander_i2c_start(struct highlander_i2c_dev *dev)
  60. {
  61. iowrite16(ioread16(dev->base + SMCR) | SMCR_START, dev->base + SMCR);
  62. }
  63. static inline void highlander_i2c_done(struct highlander_i2c_dev *dev)
  64. {
  65. iowrite16(ioread16(dev->base + SMCR) | SMCR_IRIC, dev->base + SMCR);
  66. }
  67. static void highlander_i2c_setup(struct highlander_i2c_dev *dev)
  68. {
  69. u16 smmr;
  70. smmr = ioread16(dev->base + SMMR);
  71. smmr |= SMMR_TMMD;
  72. if (iic_force_normal)
  73. smmr &= ~SMMR_SP;
  74. else
  75. smmr |= SMMR_SP;
  76. iowrite16(smmr, dev->base + SMMR);
  77. }
  78. static void smbus_write_data(u8 *src, u16 *dst, int len)
  79. {
  80. for (; len > 1; len -= 2) {
  81. *dst++ = be16_to_cpup((__be16 *)src);
  82. src += 2;
  83. }
  84. if (len)
  85. *dst = *src << 8;
  86. }
  87. static void smbus_read_data(u16 *src, u8 *dst, int len)
  88. {
  89. for (; len > 1; len -= 2) {
  90. *(__be16 *)dst = cpu_to_be16p(src++);
  91. dst += 2;
  92. }
  93. if (len)
  94. *dst = *src >> 8;
  95. }
  96. static void highlander_i2c_command(struct highlander_i2c_dev *dev,
  97. u8 command, int len)
  98. {
  99. unsigned int i;
  100. u16 cmd = (command << 8) | command;
  101. for (i = 0; i < len; i += 2) {
  102. if (len - i == 1)
  103. cmd = command << 8;
  104. iowrite16(cmd, dev->base + SMSADR + i);
  105. dev_dbg(dev->dev, "command data[%x] 0x%04x\n", i/2, cmd);
  106. }
  107. }
  108. static int highlander_i2c_wait_for_bbsy(struct highlander_i2c_dev *dev)
  109. {
  110. unsigned long timeout;
  111. timeout = jiffies + msecs_to_jiffies(iic_timeout);
  112. while (ioread16(dev->base + SMCR) & SMCR_BBSY) {
  113. if (time_after(jiffies, timeout)) {
  114. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  115. return -ETIMEDOUT;
  116. }
  117. msleep(1);
  118. }
  119. return 0;
  120. }
  121. static int highlander_i2c_reset(struct highlander_i2c_dev *dev)
  122. {
  123. iowrite16(ioread16(dev->base + SMCR) | SMCR_RST, dev->base + SMCR);
  124. return highlander_i2c_wait_for_bbsy(dev);
  125. }
  126. static int highlander_i2c_wait_for_ack(struct highlander_i2c_dev *dev)
  127. {
  128. u16 tmp = ioread16(dev->base + SMCR);
  129. if ((tmp & (SMCR_IRIC | SMCR_ACKE)) == SMCR_ACKE) {
  130. dev_warn(dev->dev, "ack abnormality\n");
  131. return highlander_i2c_reset(dev);
  132. }
  133. return 0;
  134. }
  135. static irqreturn_t highlander_i2c_irq(int irq, void *dev_id)
  136. {
  137. struct highlander_i2c_dev *dev = dev_id;
  138. highlander_i2c_done(dev);
  139. complete(&dev->cmd_complete);
  140. return IRQ_HANDLED;
  141. }
  142. static void highlander_i2c_poll(struct highlander_i2c_dev *dev)
  143. {
  144. unsigned long timeout;
  145. u16 smcr;
  146. timeout = jiffies + msecs_to_jiffies(iic_timeout);
  147. for (;;) {
  148. smcr = ioread16(dev->base + SMCR);
  149. /*
  150. * Don't bother checking ACKE here, this and the reset
  151. * are handled in highlander_i2c_wait_xfer_done() when
  152. * waiting for the ACK.
  153. */
  154. if (smcr & SMCR_IRIC)
  155. return;
  156. if (time_after(jiffies, timeout))
  157. break;
  158. cpu_relax();
  159. cond_resched();
  160. }
  161. dev_err(dev->dev, "polling timed out\n");
  162. }
  163. static inline int highlander_i2c_wait_xfer_done(struct highlander_i2c_dev *dev)
  164. {
  165. if (dev->irq)
  166. wait_for_completion_timeout(&dev->cmd_complete,
  167. msecs_to_jiffies(iic_timeout));
  168. else
  169. /* busy looping, the IRQ of champions */
  170. highlander_i2c_poll(dev);
  171. return highlander_i2c_wait_for_ack(dev);
  172. }
  173. static int highlander_i2c_read(struct highlander_i2c_dev *dev)
  174. {
  175. int i, cnt;
  176. u16 data[16];
  177. if (highlander_i2c_wait_for_bbsy(dev))
  178. return -EAGAIN;
  179. highlander_i2c_start(dev);
  180. if (highlander_i2c_wait_xfer_done(dev)) {
  181. dev_err(dev->dev, "Arbitration loss\n");
  182. return -EAGAIN;
  183. }
  184. /*
  185. * The R0P7780LC0011RL FPGA needs a significant delay between
  186. * data read cycles, otherwise the transciever gets confused and
  187. * garbage is returned when the read is subsequently aborted.
  188. *
  189. * It is not sufficient to wait for BBSY.
  190. *
  191. * While this generally only applies to the older SH7780-based
  192. * Highlanders, the same issue can be observed on SH7785 ones,
  193. * albeit less frequently. SH7780-based Highlanders may need
  194. * this to be as high as 1000 ms.
  195. */
  196. if (iic_read_delay && time_before(jiffies, dev->last_read_time +
  197. msecs_to_jiffies(iic_read_delay)))
  198. msleep(jiffies_to_msecs((dev->last_read_time +
  199. msecs_to_jiffies(iic_read_delay)) - jiffies));
  200. cnt = (dev->buf_len + 1) >> 1;
  201. for (i = 0; i < cnt; i++) {
  202. data[i] = ioread16(dev->base + SMTRDR + (i * sizeof(u16)));
  203. dev_dbg(dev->dev, "read data[%x] 0x%04x\n", i, data[i]);
  204. }
  205. smbus_read_data(data, dev->buf, dev->buf_len);
  206. dev->last_read_time = jiffies;
  207. return 0;
  208. }
  209. static int highlander_i2c_write(struct highlander_i2c_dev *dev)
  210. {
  211. int i, cnt;
  212. u16 data[16];
  213. smbus_write_data(dev->buf, data, dev->buf_len);
  214. cnt = (dev->buf_len + 1) >> 1;
  215. for (i = 0; i < cnt; i++) {
  216. iowrite16(data[i], dev->base + SMTRDR + (i * sizeof(u16)));
  217. dev_dbg(dev->dev, "write data[%x] 0x%04x\n", i, data[i]);
  218. }
  219. if (highlander_i2c_wait_for_bbsy(dev))
  220. return -EAGAIN;
  221. highlander_i2c_start(dev);
  222. return highlander_i2c_wait_xfer_done(dev);
  223. }
  224. static int highlander_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  225. unsigned short flags, char read_write,
  226. u8 command, int size,
  227. union i2c_smbus_data *data)
  228. {
  229. struct highlander_i2c_dev *dev = i2c_get_adapdata(adap);
  230. int read = read_write & I2C_SMBUS_READ;
  231. u16 tmp;
  232. init_completion(&dev->cmd_complete);
  233. dev_dbg(dev->dev, "addr %04x, command %02x, read_write %d, size %d\n",
  234. addr, command, read_write, size);
  235. /*
  236. * Set up the buffer and transfer size
  237. */
  238. switch (size) {
  239. case I2C_SMBUS_BYTE_DATA:
  240. dev->buf = &data->byte;
  241. dev->buf_len = 1;
  242. break;
  243. case I2C_SMBUS_I2C_BLOCK_DATA:
  244. dev->buf = &data->block[1];
  245. dev->buf_len = data->block[0];
  246. break;
  247. default:
  248. dev_err(dev->dev, "unsupported command %d\n", size);
  249. return -EINVAL;
  250. }
  251. /*
  252. * Encode the mode setting
  253. */
  254. tmp = ioread16(dev->base + SMMR);
  255. tmp &= ~(SMMR_MODE0 | SMMR_MODE1);
  256. switch (dev->buf_len) {
  257. case 1:
  258. /* default */
  259. break;
  260. case 8:
  261. tmp |= SMMR_MODE0;
  262. break;
  263. case 16:
  264. tmp |= SMMR_MODE1;
  265. break;
  266. case 32:
  267. tmp |= (SMMR_MODE0 | SMMR_MODE1);
  268. break;
  269. default:
  270. dev_err(dev->dev, "unsupported xfer size %d\n", dev->buf_len);
  271. return -EINVAL;
  272. }
  273. iowrite16(tmp, dev->base + SMMR);
  274. /* Ensure we're in a sane state */
  275. highlander_i2c_done(dev);
  276. /* Set slave address */
  277. iowrite16((addr << 1) | read, dev->base + SMSMADR);
  278. highlander_i2c_command(dev, command, dev->buf_len);
  279. if (read)
  280. return highlander_i2c_read(dev);
  281. else
  282. return highlander_i2c_write(dev);
  283. }
  284. static u32 highlander_i2c_func(struct i2c_adapter *adapter)
  285. {
  286. return I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
  287. }
  288. static const struct i2c_algorithm highlander_i2c_algo = {
  289. .smbus_xfer = highlander_i2c_smbus_xfer,
  290. .functionality = highlander_i2c_func,
  291. };
  292. static int __devinit highlander_i2c_probe(struct platform_device *pdev)
  293. {
  294. struct highlander_i2c_dev *dev;
  295. struct i2c_adapter *adap;
  296. struct resource *res;
  297. int ret;
  298. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  299. if (unlikely(!res)) {
  300. dev_err(&pdev->dev, "no mem resource\n");
  301. return -ENODEV;
  302. }
  303. dev = kzalloc(sizeof(struct highlander_i2c_dev), GFP_KERNEL);
  304. if (unlikely(!dev))
  305. return -ENOMEM;
  306. dev->base = ioremap_nocache(res->start, resource_size(res));
  307. if (unlikely(!dev->base)) {
  308. ret = -ENXIO;
  309. goto err;
  310. }
  311. dev->dev = &pdev->dev;
  312. platform_set_drvdata(pdev, dev);
  313. dev->irq = platform_get_irq(pdev, 0);
  314. if (iic_force_poll)
  315. dev->irq = 0;
  316. if (dev->irq) {
  317. ret = request_irq(dev->irq, highlander_i2c_irq, IRQF_DISABLED,
  318. pdev->name, dev);
  319. if (unlikely(ret))
  320. goto err_unmap;
  321. highlander_i2c_irq_enable(dev);
  322. } else {
  323. dev_notice(&pdev->dev, "no IRQ, using polling mode\n");
  324. highlander_i2c_irq_disable(dev);
  325. }
  326. dev->last_read_time = jiffies; /* initial read jiffies */
  327. highlander_i2c_setup(dev);
  328. adap = &dev->adapter;
  329. i2c_set_adapdata(adap, dev);
  330. adap->owner = THIS_MODULE;
  331. adap->class = I2C_CLASS_HWMON;
  332. strlcpy(adap->name, "HL FPGA I2C adapter", sizeof(adap->name));
  333. adap->algo = &highlander_i2c_algo;
  334. adap->dev.parent = &pdev->dev;
  335. adap->nr = pdev->id;
  336. /*
  337. * Reset the adapter
  338. */
  339. ret = highlander_i2c_reset(dev);
  340. if (unlikely(ret)) {
  341. dev_err(&pdev->dev, "controller didn't come up\n");
  342. goto err_free_irq;
  343. }
  344. ret = i2c_add_numbered_adapter(adap);
  345. if (unlikely(ret)) {
  346. dev_err(&pdev->dev, "failure adding adapter\n");
  347. goto err_free_irq;
  348. }
  349. return 0;
  350. err_free_irq:
  351. if (dev->irq)
  352. free_irq(dev->irq, dev);
  353. err_unmap:
  354. iounmap(dev->base);
  355. err:
  356. kfree(dev);
  357. platform_set_drvdata(pdev, NULL);
  358. return ret;
  359. }
  360. static int __devexit highlander_i2c_remove(struct platform_device *pdev)
  361. {
  362. struct highlander_i2c_dev *dev = platform_get_drvdata(pdev);
  363. i2c_del_adapter(&dev->adapter);
  364. if (dev->irq)
  365. free_irq(dev->irq, dev);
  366. iounmap(dev->base);
  367. kfree(dev);
  368. platform_set_drvdata(pdev, NULL);
  369. return 0;
  370. }
  371. static struct platform_driver highlander_i2c_driver = {
  372. .driver = {
  373. .name = "i2c-highlander",
  374. .owner = THIS_MODULE,
  375. },
  376. .probe = highlander_i2c_probe,
  377. .remove = __devexit_p(highlander_i2c_remove),
  378. };
  379. static int __init highlander_i2c_init(void)
  380. {
  381. return platform_driver_register(&highlander_i2c_driver);
  382. }
  383. static void __exit highlander_i2c_exit(void)
  384. {
  385. platform_driver_unregister(&highlander_i2c_driver);
  386. }
  387. module_init(highlander_i2c_init);
  388. module_exit(highlander_i2c_exit);
  389. MODULE_AUTHOR("Paul Mundt");
  390. MODULE_DESCRIPTION("Renesas Highlander FPGA I2C/SMBus adapter");
  391. MODULE_LICENSE("GPL v2");
  392. module_param(iic_force_poll, bool, 0);
  393. module_param(iic_force_normal, bool, 0);
  394. module_param(iic_timeout, int, 0);
  395. module_param(iic_read_delay, int, 0);
  396. MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
  397. MODULE_PARM_DESC(iic_force_normal,
  398. "Force normal mode (100 kHz), default is fast mode (400 kHz)");
  399. MODULE_PARM_DESC(iic_timeout, "Set timeout value in msecs (default 1000 ms)");
  400. MODULE_PARM_DESC(iic_read_delay,
  401. "Delay between data read cycles (default 0 ms)");