i2c-au1550.c 8.5 KB

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