i2c-au1550.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
  3. * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
  4. *
  5. * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
  6. *
  7. * The documentation describes this as an SMBus controller, but it doesn't
  8. * understand any of the SMBus protocol in hardware. It's really an I2C
  9. * controller that could emulate most of the SMBus in software.
  10. *
  11. * This is just a skeleton adapter to use with the Au1550 PSC
  12. * algorithm. It was developed for the Pb1550, but will work with
  13. * any Au1550 board that has a similar PSC configuration.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version 2
  18. * of the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/errno.h>
  30. #include <linux/i2c.h>
  31. #include <linux/slab.h>
  32. #include <asm/mach-au1x00/au1000.h>
  33. #include <asm/mach-au1x00/au1xxx_psc.h>
  34. #define PSC_SEL 0x00
  35. #define PSC_CTRL 0x04
  36. #define PSC_SMBCFG 0x08
  37. #define PSC_SMBMSK 0x0C
  38. #define PSC_SMBPCR 0x10
  39. #define PSC_SMBSTAT 0x14
  40. #define PSC_SMBEVNT 0x18
  41. #define PSC_SMBTXRX 0x1C
  42. #define PSC_SMBTMR 0x20
  43. struct i2c_au1550_data {
  44. void __iomem *psc_base;
  45. int xfer_timeout;
  46. struct i2c_adapter adap;
  47. };
  48. static inline void WR(struct i2c_au1550_data *a, int r, unsigned long v)
  49. {
  50. __raw_writel(v, a->psc_base + r);
  51. wmb();
  52. }
  53. static inline unsigned long RD(struct i2c_au1550_data *a, int r)
  54. {
  55. return __raw_readl(a->psc_base + r);
  56. }
  57. static int wait_xfer_done(struct i2c_au1550_data *adap)
  58. {
  59. int i;
  60. /* Wait for Tx Buffer Empty */
  61. for (i = 0; i < adap->xfer_timeout; i++) {
  62. if (RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_TE)
  63. return 0;
  64. udelay(1);
  65. }
  66. return -ETIMEDOUT;
  67. }
  68. static int wait_ack(struct i2c_au1550_data *adap)
  69. {
  70. unsigned long stat;
  71. if (wait_xfer_done(adap))
  72. return -ETIMEDOUT;
  73. stat = RD(adap, PSC_SMBEVNT);
  74. if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
  75. return -ETIMEDOUT;
  76. return 0;
  77. }
  78. static int wait_master_done(struct i2c_au1550_data *adap)
  79. {
  80. int i;
  81. /* Wait for Master Done. */
  82. for (i = 0; i < 2 * adap->xfer_timeout; i++) {
  83. if ((RD(adap, PSC_SMBEVNT) & PSC_SMBEVNT_MD) != 0)
  84. return 0;
  85. udelay(1);
  86. }
  87. return -ETIMEDOUT;
  88. }
  89. static int
  90. do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
  91. {
  92. unsigned long stat;
  93. /* Reset the FIFOs, clear events. */
  94. stat = RD(adap, PSC_SMBSTAT);
  95. WR(adap, PSC_SMBEVNT, PSC_SMBEVNT_ALLCLR);
  96. if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
  97. WR(adap, PSC_SMBPCR, PSC_SMBPCR_DC);
  98. while ((RD(adap, PSC_SMBPCR) & PSC_SMBPCR_DC) != 0)
  99. cpu_relax();
  100. udelay(50);
  101. }
  102. /* Write out the i2c chip address and specify operation */
  103. addr <<= 1;
  104. if (rd)
  105. addr |= 1;
  106. /* zero-byte xfers stop immediately */
  107. if (q)
  108. addr |= PSC_SMBTXRX_STP;
  109. /* Put byte into fifo, start up master. */
  110. WR(adap, PSC_SMBTXRX, addr);
  111. WR(adap, PSC_SMBPCR, PSC_SMBPCR_MS);
  112. if (wait_ack(adap))
  113. return -EIO;
  114. return (q) ? wait_master_done(adap) : 0;
  115. }
  116. static int wait_for_rx_byte(struct i2c_au1550_data *adap, unsigned char *out)
  117. {
  118. int j;
  119. if (wait_xfer_done(adap))
  120. return -EIO;
  121. j = adap->xfer_timeout * 100;
  122. do {
  123. j--;
  124. if (j <= 0)
  125. return -EIO;
  126. if ((RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_RE) == 0)
  127. j = 0;
  128. else
  129. udelay(1);
  130. } while (j > 0);
  131. *out = RD(adap, PSC_SMBTXRX);
  132. return 0;
  133. }
  134. static int i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
  135. unsigned int len)
  136. {
  137. int i;
  138. if (len == 0)
  139. return 0;
  140. /* A read is performed by stuffing the transmit fifo with
  141. * zero bytes for timing, waiting for bytes to appear in the
  142. * receive fifo, then reading the bytes.
  143. */
  144. i = 0;
  145. while (i < (len - 1)) {
  146. WR(adap, PSC_SMBTXRX, 0);
  147. if (wait_for_rx_byte(adap, &buf[i]))
  148. return -EIO;
  149. i++;
  150. }
  151. /* The last byte has to indicate transfer done. */
  152. WR(adap, PSC_SMBTXRX, PSC_SMBTXRX_STP);
  153. if (wait_master_done(adap))
  154. return -EIO;
  155. buf[i] = (unsigned char)(RD(adap, PSC_SMBTXRX) & 0xff);
  156. return 0;
  157. }
  158. static int i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
  159. unsigned int len)
  160. {
  161. int i;
  162. unsigned long data;
  163. if (len == 0)
  164. return 0;
  165. i = 0;
  166. while (i < (len-1)) {
  167. data = buf[i];
  168. WR(adap, PSC_SMBTXRX, data);
  169. if (wait_ack(adap))
  170. return -EIO;
  171. i++;
  172. }
  173. /* The last byte has to indicate transfer done. */
  174. data = buf[i];
  175. data |= PSC_SMBTXRX_STP;
  176. WR(adap, PSC_SMBTXRX, data);
  177. if (wait_master_done(adap))
  178. return -EIO;
  179. return 0;
  180. }
  181. static int
  182. au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  183. {
  184. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  185. struct i2c_msg *p;
  186. int i, err = 0;
  187. WR(adap, PSC_CTRL, PSC_CTRL_ENABLE);
  188. for (i = 0; !err && i < num; i++) {
  189. p = &msgs[i];
  190. err = do_address(adap, p->addr, p->flags & I2C_M_RD,
  191. (p->len == 0));
  192. if (err || !p->len)
  193. continue;
  194. if (p->flags & I2C_M_RD)
  195. err = i2c_read(adap, p->buf, p->len);
  196. else
  197. err = i2c_write(adap, p->buf, p->len);
  198. }
  199. /* Return the number of messages processed, or the error code.
  200. */
  201. if (err == 0)
  202. err = num;
  203. WR(adap, PSC_CTRL, PSC_CTRL_SUSPEND);
  204. return err;
  205. }
  206. static u32 au1550_func(struct i2c_adapter *adap)
  207. {
  208. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  209. }
  210. static const struct i2c_algorithm au1550_algo = {
  211. .master_xfer = au1550_xfer,
  212. .functionality = au1550_func,
  213. };
  214. static void i2c_au1550_setup(struct i2c_au1550_data *priv)
  215. {
  216. unsigned long cfg;
  217. WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
  218. WR(priv, PSC_SEL, PSC_SEL_PS_SMBUSMODE);
  219. WR(priv, PSC_SMBCFG, 0);
  220. WR(priv, PSC_CTRL, PSC_CTRL_ENABLE);
  221. while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
  222. cpu_relax();
  223. cfg = PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 | PSC_SMBCFG_DD_DISABLE;
  224. WR(priv, PSC_SMBCFG, cfg);
  225. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  226. * timings are based on this clock.
  227. */
  228. cfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  229. WR(priv, PSC_SMBCFG, cfg);
  230. WR(priv, PSC_SMBMSK, PSC_SMBMSK_ALLMASK);
  231. /* Set the protocol timer values. See Table 71 in the
  232. * Au1550 Data Book for standard timing values.
  233. */
  234. WR(priv, PSC_SMBTMR, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(20) | \
  235. PSC_SMBTMR_SET_PU(20) | PSC_SMBTMR_SET_SH(20) | \
  236. PSC_SMBTMR_SET_SU(20) | PSC_SMBTMR_SET_CL(20) | \
  237. PSC_SMBTMR_SET_CH(20));
  238. cfg |= PSC_SMBCFG_DE_ENABLE;
  239. WR(priv, PSC_SMBCFG, cfg);
  240. while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
  241. cpu_relax();
  242. WR(priv, PSC_CTRL, PSC_CTRL_SUSPEND);
  243. }
  244. static void i2c_au1550_disable(struct i2c_au1550_data *priv)
  245. {
  246. WR(priv, PSC_SMBCFG, 0);
  247. WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
  248. }
  249. /*
  250. * registering functions to load algorithms at runtime
  251. * Prior to calling us, the 50MHz clock frequency and routing
  252. * must have been set up for the PSC indicated by the adapter.
  253. */
  254. static int
  255. i2c_au1550_probe(struct platform_device *pdev)
  256. {
  257. struct i2c_au1550_data *priv;
  258. struct resource *r;
  259. int ret;
  260. priv = devm_kzalloc(&pdev->dev, sizeof(struct i2c_au1550_data),
  261. GFP_KERNEL);
  262. if (!priv)
  263. return -ENOMEM;
  264. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  265. priv->psc_base = devm_ioremap_resource(&pdev->dev, r);
  266. if (IS_ERR(priv->psc_base))
  267. return PTR_ERR(priv->psc_base);
  268. priv->xfer_timeout = 200;
  269. priv->adap.nr = pdev->id;
  270. priv->adap.algo = &au1550_algo;
  271. priv->adap.algo_data = priv;
  272. priv->adap.dev.parent = &pdev->dev;
  273. strlcpy(priv->adap.name, "Au1xxx PSC I2C", sizeof(priv->adap.name));
  274. /* Now, set up the PSC for SMBus PIO mode. */
  275. i2c_au1550_setup(priv);
  276. ret = i2c_add_numbered_adapter(&priv->adap);
  277. if (ret) {
  278. i2c_au1550_disable(priv);
  279. return ret;
  280. }
  281. platform_set_drvdata(pdev, priv);
  282. return 0;
  283. }
  284. static int i2c_au1550_remove(struct platform_device *pdev)
  285. {
  286. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  287. i2c_del_adapter(&priv->adap);
  288. i2c_au1550_disable(priv);
  289. return 0;
  290. }
  291. #ifdef CONFIG_PM
  292. static int i2c_au1550_suspend(struct device *dev)
  293. {
  294. struct i2c_au1550_data *priv = dev_get_drvdata(dev);
  295. i2c_au1550_disable(priv);
  296. return 0;
  297. }
  298. static int i2c_au1550_resume(struct device *dev)
  299. {
  300. struct i2c_au1550_data *priv = dev_get_drvdata(dev);
  301. i2c_au1550_setup(priv);
  302. return 0;
  303. }
  304. static const struct dev_pm_ops i2c_au1550_pmops = {
  305. .suspend = i2c_au1550_suspend,
  306. .resume = i2c_au1550_resume,
  307. };
  308. #define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)
  309. #else
  310. #define AU1XPSC_SMBUS_PMOPS NULL
  311. #endif
  312. static struct platform_driver au1xpsc_smbus_driver = {
  313. .driver = {
  314. .name = "au1xpsc_smbus",
  315. .pm = AU1XPSC_SMBUS_PMOPS,
  316. },
  317. .probe = i2c_au1550_probe,
  318. .remove = i2c_au1550_remove,
  319. };
  320. module_platform_driver(au1xpsc_smbus_driver);
  321. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  322. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  323. MODULE_LICENSE("GPL");
  324. MODULE_ALIAS("platform:au1xpsc_smbus");