i2c-highlander.c 11 KB

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