i2c-au1550.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/au1xxx.h>
  38. #include <asm/mach-au1x00/au1xxx_psc.h>
  39. struct i2c_au1550_data {
  40. u32 psc_base;
  41. int xfer_timeout;
  42. int ack_timeout;
  43. struct i2c_adapter adap;
  44. struct resource *ioarea;
  45. };
  46. static int
  47. wait_xfer_done(struct i2c_au1550_data *adap)
  48. {
  49. u32 stat;
  50. int i;
  51. volatile psc_smb_t *sp;
  52. sp = (volatile psc_smb_t *)(adap->psc_base);
  53. /* Wait for Tx Buffer Empty
  54. */
  55. for (i = 0; i < adap->xfer_timeout; i++) {
  56. stat = sp->psc_smbstat;
  57. au_sync();
  58. if ((stat & PSC_SMBSTAT_TE) != 0)
  59. return 0;
  60. udelay(1);
  61. }
  62. return -ETIMEDOUT;
  63. }
  64. static int
  65. wait_ack(struct i2c_au1550_data *adap)
  66. {
  67. u32 stat;
  68. volatile psc_smb_t *sp;
  69. if (wait_xfer_done(adap))
  70. return -ETIMEDOUT;
  71. sp = (volatile psc_smb_t *)(adap->psc_base);
  72. stat = sp->psc_smbevnt;
  73. au_sync();
  74. if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
  75. return -ETIMEDOUT;
  76. return 0;
  77. }
  78. static int
  79. wait_master_done(struct i2c_au1550_data *adap)
  80. {
  81. u32 stat;
  82. int i;
  83. volatile psc_smb_t *sp;
  84. sp = (volatile psc_smb_t *)(adap->psc_base);
  85. /* Wait for Master Done.
  86. */
  87. for (i = 0; i < adap->xfer_timeout; i++) {
  88. stat = sp->psc_smbevnt;
  89. au_sync();
  90. if ((stat & PSC_SMBEVNT_MD) != 0)
  91. return 0;
  92. udelay(1);
  93. }
  94. return -ETIMEDOUT;
  95. }
  96. static int
  97. do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
  98. {
  99. volatile psc_smb_t *sp;
  100. u32 stat;
  101. sp = (volatile psc_smb_t *)(adap->psc_base);
  102. /* Reset the FIFOs, clear events.
  103. */
  104. stat = sp->psc_smbstat;
  105. sp->psc_smbevnt = PSC_SMBEVNT_ALLCLR;
  106. au_sync();
  107. if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
  108. sp->psc_smbpcr = PSC_SMBPCR_DC;
  109. au_sync();
  110. do {
  111. stat = sp->psc_smbpcr;
  112. au_sync();
  113. } while ((stat & PSC_SMBPCR_DC) != 0);
  114. udelay(50);
  115. }
  116. /* Write out the i2c chip address and specify operation
  117. */
  118. addr <<= 1;
  119. if (rd)
  120. addr |= 1;
  121. /* zero-byte xfers stop immediately */
  122. if (q)
  123. addr |= PSC_SMBTXRX_STP;
  124. /* Put byte into fifo, start up master.
  125. */
  126. sp->psc_smbtxrx = addr;
  127. au_sync();
  128. sp->psc_smbpcr = PSC_SMBPCR_MS;
  129. au_sync();
  130. if (wait_ack(adap))
  131. return -EIO;
  132. return (q) ? wait_master_done(adap) : 0;
  133. }
  134. static u32
  135. wait_for_rx_byte(struct i2c_au1550_data *adap, u32 *ret_data)
  136. {
  137. int j;
  138. u32 data, stat;
  139. volatile psc_smb_t *sp;
  140. if (wait_xfer_done(adap))
  141. return -EIO;
  142. sp = (volatile psc_smb_t *)(adap->psc_base);
  143. j = adap->xfer_timeout * 100;
  144. do {
  145. j--;
  146. if (j <= 0)
  147. return -EIO;
  148. stat = sp->psc_smbstat;
  149. au_sync();
  150. if ((stat & PSC_SMBSTAT_RE) == 0)
  151. j = 0;
  152. else
  153. udelay(1);
  154. } while (j > 0);
  155. data = sp->psc_smbtxrx;
  156. au_sync();
  157. *ret_data = data;
  158. return 0;
  159. }
  160. static int
  161. i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
  162. unsigned int len)
  163. {
  164. int i;
  165. u32 data;
  166. volatile psc_smb_t *sp;
  167. if (len == 0)
  168. return 0;
  169. /* A read is performed by stuffing the transmit fifo with
  170. * zero bytes for timing, waiting for bytes to appear in the
  171. * receive fifo, then reading the bytes.
  172. */
  173. sp = (volatile psc_smb_t *)(adap->psc_base);
  174. i = 0;
  175. while (i < (len-1)) {
  176. sp->psc_smbtxrx = 0;
  177. au_sync();
  178. if (wait_for_rx_byte(adap, &data))
  179. return -EIO;
  180. buf[i] = data;
  181. i++;
  182. }
  183. /* The last byte has to indicate transfer done.
  184. */
  185. sp->psc_smbtxrx = PSC_SMBTXRX_STP;
  186. au_sync();
  187. if (wait_master_done(adap))
  188. return -EIO;
  189. data = sp->psc_smbtxrx;
  190. au_sync();
  191. buf[i] = data;
  192. return 0;
  193. }
  194. static int
  195. i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
  196. unsigned int len)
  197. {
  198. int i;
  199. u32 data;
  200. volatile psc_smb_t *sp;
  201. if (len == 0)
  202. return 0;
  203. sp = (volatile psc_smb_t *)(adap->psc_base);
  204. i = 0;
  205. while (i < (len-1)) {
  206. data = buf[i];
  207. sp->psc_smbtxrx = data;
  208. au_sync();
  209. if (wait_ack(adap))
  210. return -EIO;
  211. i++;
  212. }
  213. /* The last byte has to indicate transfer done.
  214. */
  215. data = buf[i];
  216. data |= PSC_SMBTXRX_STP;
  217. sp->psc_smbtxrx = data;
  218. au_sync();
  219. if (wait_master_done(adap))
  220. return -EIO;
  221. return 0;
  222. }
  223. static int
  224. au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  225. {
  226. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  227. volatile psc_smb_t *sp = (volatile psc_smb_t *)adap->psc_base;
  228. struct i2c_msg *p;
  229. int i, err = 0;
  230. sp->psc_ctrl = PSC_CTRL_ENABLE;
  231. au_sync();
  232. for (i = 0; !err && i < num; i++) {
  233. p = &msgs[i];
  234. err = do_address(adap, p->addr, p->flags & I2C_M_RD,
  235. (p->len == 0));
  236. if (err || !p->len)
  237. continue;
  238. if (p->flags & I2C_M_RD)
  239. err = i2c_read(adap, p->buf, p->len);
  240. else
  241. err = i2c_write(adap, p->buf, p->len);
  242. }
  243. /* Return the number of messages processed, or the error code.
  244. */
  245. if (err == 0)
  246. err = num;
  247. sp->psc_ctrl = PSC_CTRL_SUSPEND;
  248. au_sync();
  249. return err;
  250. }
  251. static u32
  252. au1550_func(struct i2c_adapter *adap)
  253. {
  254. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  255. }
  256. static const struct i2c_algorithm au1550_algo = {
  257. .master_xfer = au1550_xfer,
  258. .functionality = au1550_func,
  259. };
  260. static void i2c_au1550_setup(struct i2c_au1550_data *priv)
  261. {
  262. volatile psc_smb_t *sp = (volatile psc_smb_t *)priv->psc_base;
  263. u32 stat;
  264. sp->psc_ctrl = PSC_CTRL_DISABLE;
  265. au_sync();
  266. sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
  267. sp->psc_smbcfg = 0;
  268. au_sync();
  269. sp->psc_ctrl = PSC_CTRL_ENABLE;
  270. au_sync();
  271. do {
  272. stat = sp->psc_smbstat;
  273. au_sync();
  274. } while ((stat & PSC_SMBSTAT_SR) == 0);
  275. sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
  276. PSC_SMBCFG_DD_DISABLE);
  277. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  278. * timings are based on this clock.
  279. */
  280. sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  281. sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
  282. au_sync();
  283. /* Set the protocol timer values. See Table 71 in the
  284. * Au1550 Data Book for standard timing values.
  285. */
  286. sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
  287. PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
  288. PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
  289. PSC_SMBTMR_SET_CH(15);
  290. au_sync();
  291. sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
  292. do {
  293. stat = sp->psc_smbstat;
  294. au_sync();
  295. } while ((stat & PSC_SMBSTAT_SR) == 0);
  296. sp->psc_ctrl = PSC_CTRL_SUSPEND;
  297. au_sync();
  298. }
  299. static void i2c_au1550_disable(struct i2c_au1550_data *priv)
  300. {
  301. volatile psc_smb_t *sp = (volatile psc_smb_t *)priv->psc_base;
  302. sp->psc_smbcfg = 0;
  303. sp->psc_ctrl = PSC_CTRL_DISABLE;
  304. au_sync();
  305. }
  306. /*
  307. * registering functions to load algorithms at runtime
  308. * Prior to calling us, the 50MHz clock frequency and routing
  309. * must have been set up for the PSC indicated by the adapter.
  310. */
  311. static int __devinit
  312. i2c_au1550_probe(struct platform_device *pdev)
  313. {
  314. struct i2c_au1550_data *priv;
  315. struct resource *r;
  316. int ret;
  317. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  318. if (!r) {
  319. ret = -ENODEV;
  320. goto out;
  321. }
  322. priv = kzalloc(sizeof(struct i2c_au1550_data), GFP_KERNEL);
  323. if (!priv) {
  324. ret = -ENOMEM;
  325. goto out;
  326. }
  327. priv->ioarea = request_mem_region(r->start, r->end - r->start + 1,
  328. pdev->name);
  329. if (!priv->ioarea) {
  330. ret = -EBUSY;
  331. goto out_mem;
  332. }
  333. priv->psc_base = CKSEG1ADDR(r->start);
  334. priv->xfer_timeout = 200;
  335. priv->ack_timeout = 200;
  336. priv->adap.id = I2C_HW_AU1550_PSC;
  337. priv->adap.nr = pdev->id;
  338. priv->adap.algo = &au1550_algo;
  339. priv->adap.algo_data = priv;
  340. priv->adap.dev.parent = &pdev->dev;
  341. strlcpy(priv->adap.name, "Au1xxx PSC I2C", sizeof(priv->adap.name));
  342. /* Now, set up the PSC for SMBus PIO mode.
  343. */
  344. i2c_au1550_setup(priv);
  345. ret = i2c_add_numbered_adapter(&priv->adap);
  346. if (ret == 0) {
  347. platform_set_drvdata(pdev, priv);
  348. return 0;
  349. }
  350. i2c_au1550_disable(priv);
  351. release_resource(priv->ioarea);
  352. kfree(priv->ioarea);
  353. out_mem:
  354. kfree(priv);
  355. out:
  356. return ret;
  357. }
  358. static int __devexit
  359. i2c_au1550_remove(struct platform_device *pdev)
  360. {
  361. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  362. platform_set_drvdata(pdev, NULL);
  363. i2c_del_adapter(&priv->adap);
  364. i2c_au1550_disable(priv);
  365. release_resource(priv->ioarea);
  366. kfree(priv->ioarea);
  367. kfree(priv);
  368. return 0;
  369. }
  370. #ifdef CONFIG_PM
  371. static int
  372. i2c_au1550_suspend(struct platform_device *pdev, pm_message_t state)
  373. {
  374. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  375. i2c_au1550_disable(priv);
  376. return 0;
  377. }
  378. static int
  379. i2c_au1550_resume(struct platform_device *pdev)
  380. {
  381. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  382. i2c_au1550_setup(priv);
  383. return 0;
  384. }
  385. #else
  386. #define i2c_au1550_suspend NULL
  387. #define i2c_au1550_resume NULL
  388. #endif
  389. static struct platform_driver au1xpsc_smbus_driver = {
  390. .driver = {
  391. .name = "au1xpsc_smbus",
  392. .owner = THIS_MODULE,
  393. },
  394. .probe = i2c_au1550_probe,
  395. .remove = __devexit_p(i2c_au1550_remove),
  396. .suspend = i2c_au1550_suspend,
  397. .resume = i2c_au1550_resume,
  398. };
  399. static int __init
  400. i2c_au1550_init(void)
  401. {
  402. return platform_driver_register(&au1xpsc_smbus_driver);
  403. }
  404. static void __exit
  405. i2c_au1550_exit(void)
  406. {
  407. platform_driver_unregister(&au1xpsc_smbus_driver);
  408. }
  409. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  410. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  411. MODULE_LICENSE("GPL");
  412. MODULE_ALIAS("platform:au1xpsc_smbus");
  413. module_init (i2c_au1550_init);
  414. module_exit (i2c_au1550_exit);