i2c-au1550.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. struct i2c_msg *p;
  228. int i, err = 0;
  229. for (i = 0; !err && i < num; i++) {
  230. p = &msgs[i];
  231. err = do_address(adap, p->addr, p->flags & I2C_M_RD,
  232. (p->len == 0));
  233. if (err || !p->len)
  234. continue;
  235. if (p->flags & I2C_M_RD)
  236. err = i2c_read(adap, p->buf, p->len);
  237. else
  238. err = i2c_write(adap, p->buf, p->len);
  239. }
  240. /* Return the number of messages processed, or the error code.
  241. */
  242. if (err == 0)
  243. err = num;
  244. return err;
  245. }
  246. static u32
  247. au1550_func(struct i2c_adapter *adap)
  248. {
  249. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  250. }
  251. static const struct i2c_algorithm au1550_algo = {
  252. .master_xfer = au1550_xfer,
  253. .functionality = au1550_func,
  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. volatile psc_smb_t *sp;
  265. struct resource *r;
  266. u32 stat;
  267. int ret;
  268. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  269. if (!r) {
  270. ret = -ENODEV;
  271. goto out;
  272. }
  273. priv = kzalloc(sizeof(struct i2c_au1550_data), GFP_KERNEL);
  274. if (!priv) {
  275. ret = -ENOMEM;
  276. goto out;
  277. }
  278. priv->ioarea = request_mem_region(r->start, r->end - r->start + 1,
  279. pdev->name);
  280. if (!priv->ioarea) {
  281. ret = -EBUSY;
  282. goto out_mem;
  283. }
  284. priv->psc_base = CKSEG1ADDR(r->start);
  285. priv->xfer_timeout = 200;
  286. priv->ack_timeout = 200;
  287. priv->adap.id = I2C_HW_AU1550_PSC;
  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. */
  295. sp = (volatile psc_smb_t *)priv->psc_base;
  296. sp->psc_ctrl = PSC_CTRL_DISABLE;
  297. au_sync();
  298. sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
  299. sp->psc_smbcfg = 0;
  300. au_sync();
  301. sp->psc_ctrl = PSC_CTRL_ENABLE;
  302. au_sync();
  303. do {
  304. stat = sp->psc_smbstat;
  305. au_sync();
  306. } while ((stat & PSC_SMBSTAT_SR) == 0);
  307. sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
  308. PSC_SMBCFG_DD_DISABLE);
  309. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  310. * timings are based on this clock.
  311. */
  312. sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  313. sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
  314. au_sync();
  315. /* Set the protocol timer values. See Table 71 in the
  316. * Au1550 Data Book for standard timing values.
  317. */
  318. sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
  319. PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
  320. PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
  321. PSC_SMBTMR_SET_CH(15);
  322. au_sync();
  323. sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
  324. do {
  325. stat = sp->psc_smbstat;
  326. au_sync();
  327. } while ((stat & PSC_SMBSTAT_DR) == 0);
  328. ret = i2c_add_numbered_adapter(&priv->adap);
  329. if (ret == 0) {
  330. platform_set_drvdata(pdev, priv);
  331. return 0;
  332. }
  333. /* disable the PSC */
  334. sp->psc_smbcfg = 0;
  335. sp->psc_ctrl = PSC_CTRL_DISABLE;
  336. au_sync();
  337. release_resource(priv->ioarea);
  338. kfree(priv->ioarea);
  339. out_mem:
  340. kfree(priv);
  341. out:
  342. return ret;
  343. }
  344. static int __devexit
  345. i2c_au1550_remove(struct platform_device *pdev)
  346. {
  347. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  348. volatile psc_smb_t *sp = (volatile psc_smb_t *)priv->psc_base;
  349. platform_set_drvdata(pdev, NULL);
  350. i2c_del_adapter(&priv->adap);
  351. sp->psc_smbcfg = 0;
  352. sp->psc_ctrl = PSC_CTRL_DISABLE;
  353. au_sync();
  354. release_resource(priv->ioarea);
  355. kfree(priv->ioarea);
  356. kfree(priv);
  357. return 0;
  358. }
  359. static int
  360. i2c_au1550_suspend(struct platform_device *pdev, pm_message_t state)
  361. {
  362. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  363. volatile psc_smb_t *sp = (volatile psc_smb_t *)priv->psc_base;
  364. sp->psc_ctrl = PSC_CTRL_SUSPEND;
  365. au_sync();
  366. return 0;
  367. }
  368. static int
  369. i2c_au1550_resume(struct platform_device *pdev)
  370. {
  371. struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
  372. volatile psc_smb_t *sp = (volatile psc_smb_t *)priv->psc_base;
  373. sp->psc_ctrl = PSC_CTRL_ENABLE;
  374. au_sync();
  375. while (!(sp->psc_smbstat & PSC_SMBSTAT_SR))
  376. au_sync();
  377. return 0;
  378. }
  379. static struct platform_driver au1xpsc_smbus_driver = {
  380. .driver = {
  381. .name = "au1xpsc_smbus",
  382. .owner = THIS_MODULE,
  383. },
  384. .probe = i2c_au1550_probe,
  385. .remove = __devexit_p(i2c_au1550_remove),
  386. .suspend = i2c_au1550_suspend,
  387. .resume = i2c_au1550_resume,
  388. };
  389. static int __init
  390. i2c_au1550_init(void)
  391. {
  392. return platform_driver_register(&au1xpsc_smbus_driver);
  393. }
  394. static void __exit
  395. i2c_au1550_exit(void)
  396. {
  397. platform_driver_unregister(&au1xpsc_smbus_driver);
  398. }
  399. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  400. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  401. MODULE_LICENSE("GPL");
  402. MODULE_ALIAS("platform:au1xpsc_smbus");
  403. module_init (i2c_au1550_init);
  404. module_exit (i2c_au1550_exit);