i2c-au1550.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/init.h>
  33. #include <linux/errno.h>
  34. #include <linux/i2c.h>
  35. #include <asm/mach-au1x00/au1xxx.h>
  36. #include <asm/mach-au1x00/au1xxx_psc.h>
  37. #include "i2c-au1550.h"
  38. static int
  39. wait_xfer_done(struct i2c_au1550_data *adap)
  40. {
  41. u32 stat;
  42. int i;
  43. volatile psc_smb_t *sp;
  44. sp = (volatile psc_smb_t *)(adap->psc_base);
  45. /* Wait for Tx Buffer Empty
  46. */
  47. for (i = 0; i < adap->xfer_timeout; i++) {
  48. stat = sp->psc_smbstat;
  49. au_sync();
  50. if ((stat & PSC_SMBSTAT_TE) != 0)
  51. return 0;
  52. udelay(1);
  53. }
  54. return -ETIMEDOUT;
  55. }
  56. static int
  57. wait_ack(struct i2c_au1550_data *adap)
  58. {
  59. u32 stat;
  60. volatile psc_smb_t *sp;
  61. if (wait_xfer_done(adap))
  62. return -ETIMEDOUT;
  63. sp = (volatile psc_smb_t *)(adap->psc_base);
  64. stat = sp->psc_smbevnt;
  65. au_sync();
  66. if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
  67. return -ETIMEDOUT;
  68. return 0;
  69. }
  70. static int
  71. wait_master_done(struct i2c_au1550_data *adap)
  72. {
  73. u32 stat;
  74. int i;
  75. volatile psc_smb_t *sp;
  76. sp = (volatile psc_smb_t *)(adap->psc_base);
  77. /* Wait for Master Done.
  78. */
  79. for (i = 0; i < adap->xfer_timeout; i++) {
  80. stat = sp->psc_smbevnt;
  81. au_sync();
  82. if ((stat & PSC_SMBEVNT_MD) != 0)
  83. return 0;
  84. udelay(1);
  85. }
  86. return -ETIMEDOUT;
  87. }
  88. static int
  89. do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
  90. {
  91. volatile psc_smb_t *sp;
  92. u32 stat;
  93. sp = (volatile psc_smb_t *)(adap->psc_base);
  94. /* Reset the FIFOs, clear events.
  95. */
  96. stat = sp->psc_smbstat;
  97. sp->psc_smbevnt = PSC_SMBEVNT_ALLCLR;
  98. au_sync();
  99. if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
  100. sp->psc_smbpcr = PSC_SMBPCR_DC;
  101. au_sync();
  102. do {
  103. stat = sp->psc_smbpcr;
  104. au_sync();
  105. } while ((stat & PSC_SMBPCR_DC) != 0);
  106. udelay(50);
  107. }
  108. /* Write out the i2c chip address and specify operation
  109. */
  110. addr <<= 1;
  111. if (rd)
  112. addr |= 1;
  113. /* zero-byte xfers stop immediately */
  114. if (q)
  115. addr |= PSC_SMBTXRX_STP;
  116. /* Put byte into fifo, start up master.
  117. */
  118. sp->psc_smbtxrx = addr;
  119. au_sync();
  120. sp->psc_smbpcr = PSC_SMBPCR_MS;
  121. au_sync();
  122. if (wait_ack(adap))
  123. return -EIO;
  124. return (q) ? wait_master_done(adap) : 0;
  125. }
  126. static u32
  127. wait_for_rx_byte(struct i2c_au1550_data *adap, u32 *ret_data)
  128. {
  129. int j;
  130. u32 data, stat;
  131. volatile psc_smb_t *sp;
  132. if (wait_xfer_done(adap))
  133. return -EIO;
  134. sp = (volatile psc_smb_t *)(adap->psc_base);
  135. j = adap->xfer_timeout * 100;
  136. do {
  137. j--;
  138. if (j <= 0)
  139. return -EIO;
  140. stat = sp->psc_smbstat;
  141. au_sync();
  142. if ((stat & PSC_SMBSTAT_RE) == 0)
  143. j = 0;
  144. else
  145. udelay(1);
  146. } while (j > 0);
  147. data = sp->psc_smbtxrx;
  148. au_sync();
  149. *ret_data = data;
  150. return 0;
  151. }
  152. static int
  153. i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
  154. unsigned int len)
  155. {
  156. int i;
  157. u32 data;
  158. volatile psc_smb_t *sp;
  159. if (len == 0)
  160. return 0;
  161. /* A read is performed by stuffing the transmit fifo with
  162. * zero bytes for timing, waiting for bytes to appear in the
  163. * receive fifo, then reading the bytes.
  164. */
  165. sp = (volatile psc_smb_t *)(adap->psc_base);
  166. i = 0;
  167. while (i < (len-1)) {
  168. sp->psc_smbtxrx = 0;
  169. au_sync();
  170. if (wait_for_rx_byte(adap, &data))
  171. return -EIO;
  172. buf[i] = data;
  173. i++;
  174. }
  175. /* The last byte has to indicate transfer done.
  176. */
  177. sp->psc_smbtxrx = PSC_SMBTXRX_STP;
  178. au_sync();
  179. if (wait_master_done(adap))
  180. return -EIO;
  181. data = sp->psc_smbtxrx;
  182. au_sync();
  183. buf[i] = data;
  184. return 0;
  185. }
  186. static int
  187. i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
  188. unsigned int len)
  189. {
  190. int i;
  191. u32 data;
  192. volatile psc_smb_t *sp;
  193. if (len == 0)
  194. return 0;
  195. sp = (volatile psc_smb_t *)(adap->psc_base);
  196. i = 0;
  197. while (i < (len-1)) {
  198. data = buf[i];
  199. sp->psc_smbtxrx = data;
  200. au_sync();
  201. if (wait_ack(adap))
  202. return -EIO;
  203. i++;
  204. }
  205. /* The last byte has to indicate transfer done.
  206. */
  207. data = buf[i];
  208. data |= PSC_SMBTXRX_STP;
  209. sp->psc_smbtxrx = data;
  210. au_sync();
  211. if (wait_master_done(adap))
  212. return -EIO;
  213. return 0;
  214. }
  215. static int
  216. au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  217. {
  218. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  219. struct i2c_msg *p;
  220. int i, err = 0;
  221. for (i = 0; !err && i < num; i++) {
  222. p = &msgs[i];
  223. err = do_address(adap, p->addr, p->flags & I2C_M_RD,
  224. (p->len == 0));
  225. if (err || !p->len)
  226. continue;
  227. if (p->flags & I2C_M_RD)
  228. err = i2c_read(adap, p->buf, p->len);
  229. else
  230. err = i2c_write(adap, p->buf, p->len);
  231. }
  232. /* Return the number of messages processed, or the error code.
  233. */
  234. if (err == 0)
  235. err = num;
  236. return err;
  237. }
  238. static u32
  239. au1550_func(struct i2c_adapter *adap)
  240. {
  241. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  242. }
  243. static const struct i2c_algorithm au1550_algo = {
  244. .master_xfer = au1550_xfer,
  245. .functionality = au1550_func,
  246. };
  247. /*
  248. * registering functions to load algorithms at runtime
  249. * Prior to calling us, the 50MHz clock frequency and routing
  250. * must have been set up for the PSC indicated by the adapter.
  251. */
  252. int
  253. i2c_au1550_add_bus(struct i2c_adapter *i2c_adap)
  254. {
  255. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  256. volatile psc_smb_t *sp;
  257. u32 stat;
  258. i2c_adap->algo = &au1550_algo;
  259. /* Now, set up the PSC for SMBus PIO mode.
  260. */
  261. sp = (volatile psc_smb_t *)(adap->psc_base);
  262. sp->psc_ctrl = PSC_CTRL_DISABLE;
  263. au_sync();
  264. sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
  265. sp->psc_smbcfg = 0;
  266. au_sync();
  267. sp->psc_ctrl = PSC_CTRL_ENABLE;
  268. au_sync();
  269. do {
  270. stat = sp->psc_smbstat;
  271. au_sync();
  272. } while ((stat & PSC_SMBSTAT_SR) == 0);
  273. sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
  274. PSC_SMBCFG_DD_DISABLE);
  275. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  276. * timings are based on this clock.
  277. */
  278. sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  279. sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
  280. au_sync();
  281. /* Set the protocol timer values. See Table 71 in the
  282. * Au1550 Data Book for standard timing values.
  283. */
  284. sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
  285. PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
  286. PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
  287. PSC_SMBTMR_SET_CH(15);
  288. au_sync();
  289. sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
  290. do {
  291. stat = sp->psc_smbstat;
  292. au_sync();
  293. } while ((stat & PSC_SMBSTAT_DR) == 0);
  294. return i2c_add_adapter(i2c_adap);
  295. }
  296. int
  297. i2c_au1550_del_bus(struct i2c_adapter *adap)
  298. {
  299. return i2c_del_adapter(adap);
  300. }
  301. static int
  302. pb1550_reg(struct i2c_client *client)
  303. {
  304. return 0;
  305. }
  306. static int
  307. pb1550_unreg(struct i2c_client *client)
  308. {
  309. return 0;
  310. }
  311. static struct i2c_au1550_data pb1550_i2c_info = {
  312. SMBUS_PSC_BASE, 200, 200
  313. };
  314. static struct i2c_adapter pb1550_board_adapter = {
  315. name: "pb1550 adapter",
  316. id: I2C_HW_AU1550_PSC,
  317. algo: NULL,
  318. algo_data: &pb1550_i2c_info,
  319. client_register: pb1550_reg,
  320. client_unregister: pb1550_unreg,
  321. };
  322. /* BIG hack to support the control interface on the Wolfson WM8731
  323. * audio codec on the Pb1550 board. We get an address and two data
  324. * bytes to write, create an i2c message, and send it across the
  325. * i2c transfer function. We do this here because we have access to
  326. * the i2c adapter structure.
  327. */
  328. static struct i2c_msg wm_i2c_msg; /* We don't want this stuff on the stack */
  329. static u8 i2cbuf[2];
  330. int
  331. pb1550_wm_codec_write(u8 addr, u8 reg, u8 val)
  332. {
  333. wm_i2c_msg.addr = addr;
  334. wm_i2c_msg.flags = 0;
  335. wm_i2c_msg.buf = i2cbuf;
  336. wm_i2c_msg.len = 2;
  337. i2cbuf[0] = reg;
  338. i2cbuf[1] = val;
  339. return pb1550_board_adapter.algo->master_xfer(&pb1550_board_adapter, &wm_i2c_msg, 1);
  340. }
  341. static int __init
  342. i2c_au1550_init(void)
  343. {
  344. printk(KERN_INFO "Au1550 I2C: ");
  345. /* This is where we would set up a 50MHz clock source
  346. * and routing. On the Pb1550, the SMBus is PSC2, which
  347. * uses a shared clock with USB. This has been already
  348. * configured by Yamon as a 48MHz clock, close enough
  349. * for our work.
  350. */
  351. if (i2c_au1550_add_bus(&pb1550_board_adapter) < 0) {
  352. printk("failed to initialize.\n");
  353. return -ENODEV;
  354. }
  355. printk("initialized.\n");
  356. return 0;
  357. }
  358. static void __exit
  359. i2c_au1550_exit(void)
  360. {
  361. i2c_au1550_del_bus(&pb1550_board_adapter);
  362. }
  363. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  364. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  365. MODULE_LICENSE("GPL");
  366. module_init (i2c_au1550_init);
  367. module_exit (i2c_au1550_exit);