i2c-au1550.c 9.2 KB

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