i2c-au1550.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. struct resource *ioarea;
  48. };
  49. static inline void WR(struct i2c_au1550_data *a, int r, unsigned long v)
  50. {
  51. __raw_writel(v, a->psc_base + r);
  52. wmb();
  53. }
  54. static inline unsigned long RD(struct i2c_au1550_data *a, int r)
  55. {
  56. return __raw_readl(a->psc_base + r);
  57. }
  58. static int wait_xfer_done(struct i2c_au1550_data *adap)
  59. {
  60. int i;
  61. /* Wait for Tx Buffer Empty */
  62. for (i = 0; i < adap->xfer_timeout; i++) {
  63. if (RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_TE)
  64. return 0;
  65. udelay(1);
  66. }
  67. return -ETIMEDOUT;
  68. }
  69. static int wait_ack(struct i2c_au1550_data *adap)
  70. {
  71. unsigned long stat;
  72. if (wait_xfer_done(adap))
  73. return -ETIMEDOUT;
  74. stat = RD(adap, PSC_SMBEVNT);
  75. if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
  76. return -ETIMEDOUT;
  77. return 0;
  78. }
  79. static int wait_master_done(struct i2c_au1550_data *adap)
  80. {
  81. int i;
  82. /* Wait for Master Done. */
  83. for (i = 0; i < 2 * adap->xfer_timeout; i++) {
  84. if ((RD(adap, PSC_SMBEVNT) & PSC_SMBEVNT_MD) != 0)
  85. return 0;
  86. udelay(1);
  87. }
  88. return -ETIMEDOUT;
  89. }
  90. static int
  91. do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
  92. {
  93. unsigned long stat;
  94. /* Reset the FIFOs, clear events. */
  95. stat = RD(adap, PSC_SMBSTAT);
  96. WR(adap, PSC_SMBEVNT, PSC_SMBEVNT_ALLCLR);
  97. if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
  98. WR(adap, PSC_SMBPCR, PSC_SMBPCR_DC);
  99. while ((RD(adap, PSC_SMBPCR) & PSC_SMBPCR_DC) != 0)
  100. cpu_relax();
  101. udelay(50);
  102. }
  103. /* Write out the i2c chip address and specify operation */
  104. addr <<= 1;
  105. if (rd)
  106. addr |= 1;
  107. /* zero-byte xfers stop immediately */
  108. if (q)
  109. addr |= PSC_SMBTXRX_STP;
  110. /* Put byte into fifo, start up master. */
  111. WR(adap, PSC_SMBTXRX, addr);
  112. WR(adap, PSC_SMBPCR, PSC_SMBPCR_MS);
  113. if (wait_ack(adap))
  114. return -EIO;
  115. return (q) ? wait_master_done(adap) : 0;
  116. }
  117. static int wait_for_rx_byte(struct i2c_au1550_data *adap, unsigned char *out)
  118. {
  119. int j;
  120. if (wait_xfer_done(adap))
  121. return -EIO;
  122. j = adap->xfer_timeout * 100;
  123. do {
  124. j--;
  125. if (j <= 0)
  126. return -EIO;
  127. if ((RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_RE) == 0)
  128. j = 0;
  129. else
  130. udelay(1);
  131. } while (j > 0);
  132. *out = RD(adap, PSC_SMBTXRX);
  133. return 0;
  134. }
  135. static int i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
  136. unsigned int len)
  137. {
  138. int i;
  139. if (len == 0)
  140. return 0;
  141. /* A read is performed by stuffing the transmit fifo with
  142. * zero bytes for timing, waiting for bytes to appear in the
  143. * receive fifo, then reading the bytes.
  144. */
  145. i = 0;
  146. while (i < (len - 1)) {
  147. WR(adap, PSC_SMBTXRX, 0);
  148. if (wait_for_rx_byte(adap, &buf[i]))
  149. return -EIO;
  150. i++;
  151. }
  152. /* The last byte has to indicate transfer done. */
  153. WR(adap, PSC_SMBTXRX, PSC_SMBTXRX_STP);
  154. if (wait_master_done(adap))
  155. return -EIO;
  156. buf[i] = (unsigned char)(RD(adap, PSC_SMBTXRX) & 0xff);
  157. return 0;
  158. }
  159. static int i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
  160. unsigned int len)
  161. {
  162. int i;
  163. unsigned long data;
  164. if (len == 0)
  165. return 0;
  166. i = 0;
  167. while (i < (len-1)) {
  168. data = buf[i];
  169. WR(adap, PSC_SMBTXRX, data);
  170. if (wait_ack(adap))
  171. return -EIO;
  172. i++;
  173. }
  174. /* The last byte has to indicate transfer done. */
  175. data = buf[i];
  176. data |= PSC_SMBTXRX_STP;
  177. WR(adap, PSC_SMBTXRX, data);
  178. if (wait_master_done(adap))
  179. return -EIO;
  180. return 0;
  181. }
  182. static int
  183. au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  184. {
  185. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  186. struct i2c_msg *p;
  187. int i, err = 0;
  188. WR(adap, PSC_CTRL, PSC_CTRL_ENABLE);
  189. for (i = 0; !err && i < num; i++) {
  190. p = &msgs[i];
  191. err = do_address(adap, p->addr, p->flags & I2C_M_RD,
  192. (p->len == 0));
  193. if (err || !p->len)
  194. continue;
  195. if (p->flags & I2C_M_RD)
  196. err = i2c_read(adap, p->buf, p->len);
  197. else
  198. err = i2c_write(adap, p->buf, p->len);
  199. }
  200. /* Return the number of messages processed, or the error code.
  201. */
  202. if (err == 0)
  203. err = num;
  204. WR(adap, PSC_CTRL, PSC_CTRL_SUSPEND);
  205. return err;
  206. }
  207. static u32 au1550_func(struct i2c_adapter *adap)
  208. {
  209. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  210. }
  211. static const struct i2c_algorithm au1550_algo = {
  212. .master_xfer = au1550_xfer,
  213. .functionality = au1550_func,
  214. };
  215. static void i2c_au1550_setup(struct i2c_au1550_data *priv)
  216. {
  217. unsigned long cfg;
  218. WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
  219. WR(priv, PSC_SEL, PSC_SEL_PS_SMBUSMODE);
  220. WR(priv, PSC_SMBCFG, 0);
  221. WR(priv, PSC_CTRL, PSC_CTRL_ENABLE);
  222. while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
  223. cpu_relax();
  224. cfg = PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 | PSC_SMBCFG_DD_DISABLE;
  225. WR(priv, PSC_SMBCFG, cfg);
  226. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  227. * timings are based on this clock.
  228. */
  229. cfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  230. WR(priv, PSC_SMBCFG, cfg);
  231. WR(priv, PSC_SMBMSK, PSC_SMBMSK_ALLMASK);
  232. /* Set the protocol timer values. See Table 71 in the
  233. * Au1550 Data Book for standard timing values.
  234. */
  235. WR(priv, PSC_SMBTMR, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
  236. PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
  237. PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
  238. PSC_SMBTMR_SET_CH(15));
  239. cfg |= PSC_SMBCFG_DE_ENABLE;
  240. WR(priv, PSC_SMBCFG, cfg);
  241. while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
  242. cpu_relax();
  243. WR(priv, PSC_CTRL, PSC_CTRL_SUSPEND);
  244. }
  245. static void i2c_au1550_disable(struct i2c_au1550_data *priv)
  246. {
  247. WR(priv, PSC_SMBCFG, 0);
  248. WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
  249. }
  250. /*
  251. * registering functions to load algorithms at runtime
  252. * Prior to calling us, the 50MHz clock frequency and routing
  253. * must have been set up for the PSC indicated by the adapter.
  254. */
  255. static int
  256. i2c_au1550_probe(struct platform_device *pdev)
  257. {
  258. struct i2c_au1550_data *priv;
  259. struct resource *r;
  260. int ret;
  261. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  262. if (!r) {
  263. ret = -ENODEV;
  264. goto out;
  265. }
  266. priv = kzalloc(sizeof(struct i2c_au1550_data), GFP_KERNEL);
  267. if (!priv) {
  268. ret = -ENOMEM;
  269. goto out;
  270. }
  271. priv->ioarea = request_mem_region(r->start, resource_size(r),
  272. pdev->name);
  273. if (!priv->ioarea) {
  274. ret = -EBUSY;
  275. goto out_mem;
  276. }
  277. priv->psc_base = ioremap(r->start, resource_size(r));
  278. if (!priv->psc_base) {
  279. ret = -EIO;
  280. goto out_map;
  281. }
  282. priv->xfer_timeout = 200;
  283. priv->adap.nr = pdev->id;
  284. priv->adap.algo = &au1550_algo;
  285. priv->adap.algo_data = priv;
  286. priv->adap.dev.parent = &pdev->dev;
  287. strlcpy(priv->adap.name, "Au1xxx PSC I2C", sizeof(priv->adap.name));
  288. /* Now, set up the PSC for SMBus PIO mode. */
  289. i2c_au1550_setup(priv);
  290. ret = i2c_add_numbered_adapter(&priv->adap);
  291. if (ret == 0) {
  292. platform_set_drvdata(pdev, priv);
  293. return 0;
  294. }
  295. i2c_au1550_disable(priv);
  296. iounmap(priv->psc_base);
  297. out_map:
  298. release_resource(priv->ioarea);
  299. kfree(priv->ioarea);
  300. out_mem:
  301. kfree(priv);
  302. out:
  303. return ret;
  304. }
  305. static int i2c_au1550_remove(struct platform_device *pdev)
  306. {
  307. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  308. i2c_del_adapter(&priv->adap);
  309. i2c_au1550_disable(priv);
  310. iounmap(priv->psc_base);
  311. release_resource(priv->ioarea);
  312. kfree(priv->ioarea);
  313. kfree(priv);
  314. return 0;
  315. }
  316. #ifdef CONFIG_PM
  317. static int i2c_au1550_suspend(struct device *dev)
  318. {
  319. struct i2c_au1550_data *priv = dev_get_drvdata(dev);
  320. i2c_au1550_disable(priv);
  321. return 0;
  322. }
  323. static int i2c_au1550_resume(struct device *dev)
  324. {
  325. struct i2c_au1550_data *priv = dev_get_drvdata(dev);
  326. i2c_au1550_setup(priv);
  327. return 0;
  328. }
  329. static const struct dev_pm_ops i2c_au1550_pmops = {
  330. .suspend = i2c_au1550_suspend,
  331. .resume = i2c_au1550_resume,
  332. };
  333. #define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)
  334. #else
  335. #define AU1XPSC_SMBUS_PMOPS NULL
  336. #endif
  337. static struct platform_driver au1xpsc_smbus_driver = {
  338. .driver = {
  339. .name = "au1xpsc_smbus",
  340. .pm = AU1XPSC_SMBUS_PMOPS,
  341. },
  342. .probe = i2c_au1550_probe,
  343. .remove = i2c_au1550_remove,
  344. };
  345. module_platform_driver(au1xpsc_smbus_driver);
  346. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  347. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  348. MODULE_LICENSE("GPL");
  349. MODULE_ALIAS("platform:au1xpsc_smbus");