i2c-au1550.c 9.3 KB

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