i2c-au1550.c 9.7 KB

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