i2c-i801.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
  3. Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
  4. <mdsxyz123@yahoo.com>
  5. Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. Supports the following Intel I/O Controller Hubs (ICH):
  20. I/O Block I2C
  21. region SMBus Block proc. block
  22. Chip name PCI ID size PEC buffer call read
  23. ----------------------------------------------------------------------
  24. 82801AA (ICH) 0x2413 16 no no no no
  25. 82801AB (ICH0) 0x2423 16 no no no no
  26. 82801BA (ICH2) 0x2443 16 no no no no
  27. 82801CA (ICH3) 0x2483 32 soft no no no
  28. 82801DB (ICH4) 0x24c3 32 hard yes no no
  29. 82801E (ICH5) 0x24d3 32 hard yes yes yes
  30. 6300ESB 0x25a4 32 hard yes yes yes
  31. 82801F (ICH6) 0x266a 32 hard yes yes yes
  32. 6310ESB/6320ESB 0x269b 32 hard yes yes yes
  33. 82801G (ICH7) 0x27da 32 hard yes yes yes
  34. 82801H (ICH8) 0x283e 32 hard yes yes yes
  35. 82801I (ICH9) 0x2930 32 hard yes yes yes
  36. Tolapai 0x5032 32 hard yes yes yes
  37. ICH10 0x3a30 32 hard yes yes yes
  38. ICH10 0x3a60 32 hard yes yes yes
  39. 3400/5 Series (PCH) 0x3b30 32 hard yes yes yes
  40. Cougar Point (PCH) 0x1c22 32 hard yes yes yes
  41. Features supported by this driver:
  42. Software PEC no
  43. Hardware PEC yes
  44. Block buffer yes
  45. Block process call transaction no
  46. I2C block read transaction yes (doesn't use the block buffer)
  47. See the file Documentation/i2c/busses/i2c-i801 for details.
  48. */
  49. /* Note: we assume there can only be one I801, with one SMBus interface */
  50. #include <linux/module.h>
  51. #include <linux/pci.h>
  52. #include <linux/kernel.h>
  53. #include <linux/stddef.h>
  54. #include <linux/delay.h>
  55. #include <linux/ioport.h>
  56. #include <linux/init.h>
  57. #include <linux/i2c.h>
  58. #include <linux/acpi.h>
  59. #include <linux/io.h>
  60. #include <linux/dmi.h>
  61. /* I801 SMBus address offsets */
  62. #define SMBHSTSTS (0 + i801_smba)
  63. #define SMBHSTCNT (2 + i801_smba)
  64. #define SMBHSTCMD (3 + i801_smba)
  65. #define SMBHSTADD (4 + i801_smba)
  66. #define SMBHSTDAT0 (5 + i801_smba)
  67. #define SMBHSTDAT1 (6 + i801_smba)
  68. #define SMBBLKDAT (7 + i801_smba)
  69. #define SMBPEC (8 + i801_smba) /* ICH3 and later */
  70. #define SMBAUXSTS (12 + i801_smba) /* ICH4 and later */
  71. #define SMBAUXCTL (13 + i801_smba) /* ICH4 and later */
  72. /* PCI Address Constants */
  73. #define SMBBAR 4
  74. #define SMBHSTCFG 0x040
  75. /* Host configuration bits for SMBHSTCFG */
  76. #define SMBHSTCFG_HST_EN 1
  77. #define SMBHSTCFG_SMB_SMI_EN 2
  78. #define SMBHSTCFG_I2C_EN 4
  79. /* Auxillary control register bits, ICH4+ only */
  80. #define SMBAUXCTL_CRC 1
  81. #define SMBAUXCTL_E32B 2
  82. /* kill bit for SMBHSTCNT */
  83. #define SMBHSTCNT_KILL 2
  84. /* Other settings */
  85. #define MAX_TIMEOUT 100
  86. #define ENABLE_INT9 0 /* set to 0x01 to enable - untested */
  87. /* I801 command constants */
  88. #define I801_QUICK 0x00
  89. #define I801_BYTE 0x04
  90. #define I801_BYTE_DATA 0x08
  91. #define I801_WORD_DATA 0x0C
  92. #define I801_PROC_CALL 0x10 /* unimplemented */
  93. #define I801_BLOCK_DATA 0x14
  94. #define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
  95. #define I801_BLOCK_LAST 0x34
  96. #define I801_I2C_BLOCK_LAST 0x38 /* ICH5 and later */
  97. #define I801_START 0x40
  98. #define I801_PEC_EN 0x80 /* ICH3 and later */
  99. /* I801 Hosts Status register bits */
  100. #define SMBHSTSTS_BYTE_DONE 0x80
  101. #define SMBHSTSTS_INUSE_STS 0x40
  102. #define SMBHSTSTS_SMBALERT_STS 0x20
  103. #define SMBHSTSTS_FAILED 0x10
  104. #define SMBHSTSTS_BUS_ERR 0x08
  105. #define SMBHSTSTS_DEV_ERR 0x04
  106. #define SMBHSTSTS_INTR 0x02
  107. #define SMBHSTSTS_HOST_BUSY 0x01
  108. #define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_FAILED | \
  109. SMBHSTSTS_BUS_ERR | SMBHSTSTS_DEV_ERR | \
  110. SMBHSTSTS_INTR)
  111. static unsigned long i801_smba;
  112. static unsigned char i801_original_hstcfg;
  113. static struct pci_driver i801_driver;
  114. static struct pci_dev *I801_dev;
  115. #define FEATURE_SMBUS_PEC (1 << 0)
  116. #define FEATURE_BLOCK_BUFFER (1 << 1)
  117. #define FEATURE_BLOCK_PROC (1 << 2)
  118. #define FEATURE_I2C_BLOCK_READ (1 << 3)
  119. static unsigned int i801_features;
  120. static const char *i801_feature_names[] = {
  121. "SMBus PEC",
  122. "Block buffer",
  123. "Block process call",
  124. "I2C block read",
  125. };
  126. static unsigned int disable_features;
  127. module_param(disable_features, uint, S_IRUGO | S_IWUSR);
  128. MODULE_PARM_DESC(disable_features, "Disable selected driver features");
  129. /* Make sure the SMBus host is ready to start transmitting.
  130. Return 0 if it is, -EBUSY if it is not. */
  131. static int i801_check_pre(void)
  132. {
  133. int status;
  134. status = inb_p(SMBHSTSTS);
  135. if (status & SMBHSTSTS_HOST_BUSY) {
  136. dev_err(&I801_dev->dev, "SMBus is busy, can't use it!\n");
  137. return -EBUSY;
  138. }
  139. status &= STATUS_FLAGS;
  140. if (status) {
  141. dev_dbg(&I801_dev->dev, "Clearing status flags (%02x)\n",
  142. status);
  143. outb_p(status, SMBHSTSTS);
  144. status = inb_p(SMBHSTSTS) & STATUS_FLAGS;
  145. if (status) {
  146. dev_err(&I801_dev->dev,
  147. "Failed clearing status flags (%02x)\n",
  148. status);
  149. return -EBUSY;
  150. }
  151. }
  152. return 0;
  153. }
  154. /* Convert the status register to an error code, and clear it. */
  155. static int i801_check_post(int status, int timeout)
  156. {
  157. int result = 0;
  158. /* If the SMBus is still busy, we give up */
  159. if (timeout) {
  160. dev_err(&I801_dev->dev, "Transaction timeout\n");
  161. /* try to stop the current command */
  162. dev_dbg(&I801_dev->dev, "Terminating the current operation\n");
  163. outb_p(inb_p(SMBHSTCNT) | SMBHSTCNT_KILL, SMBHSTCNT);
  164. msleep(1);
  165. outb_p(inb_p(SMBHSTCNT) & (~SMBHSTCNT_KILL), SMBHSTCNT);
  166. /* Check if it worked */
  167. status = inb_p(SMBHSTSTS);
  168. if ((status & SMBHSTSTS_HOST_BUSY) ||
  169. !(status & SMBHSTSTS_FAILED))
  170. dev_err(&I801_dev->dev,
  171. "Failed terminating the transaction\n");
  172. outb_p(STATUS_FLAGS, SMBHSTSTS);
  173. return -ETIMEDOUT;
  174. }
  175. if (status & SMBHSTSTS_FAILED) {
  176. result = -EIO;
  177. dev_err(&I801_dev->dev, "Transaction failed\n");
  178. }
  179. if (status & SMBHSTSTS_DEV_ERR) {
  180. result = -ENXIO;
  181. dev_dbg(&I801_dev->dev, "No response\n");
  182. }
  183. if (status & SMBHSTSTS_BUS_ERR) {
  184. result = -EAGAIN;
  185. dev_dbg(&I801_dev->dev, "Lost arbitration\n");
  186. }
  187. if (result) {
  188. /* Clear error flags */
  189. outb_p(status & STATUS_FLAGS, SMBHSTSTS);
  190. status = inb_p(SMBHSTSTS) & STATUS_FLAGS;
  191. if (status) {
  192. dev_warn(&I801_dev->dev, "Failed clearing status "
  193. "flags at end of transaction (%02x)\n",
  194. status);
  195. }
  196. }
  197. return result;
  198. }
  199. static int i801_transaction(int xact)
  200. {
  201. int status;
  202. int result;
  203. int timeout = 0;
  204. result = i801_check_pre();
  205. if (result < 0)
  206. return result;
  207. /* the current contents of SMBHSTCNT can be overwritten, since PEC,
  208. * INTREN, SMBSCMD are passed in xact */
  209. outb_p(xact | I801_START, SMBHSTCNT);
  210. /* We will always wait for a fraction of a second! */
  211. do {
  212. msleep(1);
  213. status = inb_p(SMBHSTSTS);
  214. } while ((status & SMBHSTSTS_HOST_BUSY) && (timeout++ < MAX_TIMEOUT));
  215. result = i801_check_post(status, timeout > MAX_TIMEOUT);
  216. if (result < 0)
  217. return result;
  218. outb_p(SMBHSTSTS_INTR, SMBHSTSTS);
  219. return 0;
  220. }
  221. /* wait for INTR bit as advised by Intel */
  222. static void i801_wait_hwpec(void)
  223. {
  224. int timeout = 0;
  225. int status;
  226. do {
  227. msleep(1);
  228. status = inb_p(SMBHSTSTS);
  229. } while ((!(status & SMBHSTSTS_INTR))
  230. && (timeout++ < MAX_TIMEOUT));
  231. if (timeout > MAX_TIMEOUT)
  232. dev_dbg(&I801_dev->dev, "PEC Timeout!\n");
  233. outb_p(status, SMBHSTSTS);
  234. }
  235. static int i801_block_transaction_by_block(union i2c_smbus_data *data,
  236. char read_write, int hwpec)
  237. {
  238. int i, len;
  239. int status;
  240. inb_p(SMBHSTCNT); /* reset the data buffer index */
  241. /* Use 32-byte buffer to process this transaction */
  242. if (read_write == I2C_SMBUS_WRITE) {
  243. len = data->block[0];
  244. outb_p(len, SMBHSTDAT0);
  245. for (i = 0; i < len; i++)
  246. outb_p(data->block[i+1], SMBBLKDAT);
  247. }
  248. status = i801_transaction(I801_BLOCK_DATA | ENABLE_INT9 |
  249. I801_PEC_EN * hwpec);
  250. if (status)
  251. return status;
  252. if (read_write == I2C_SMBUS_READ) {
  253. len = inb_p(SMBHSTDAT0);
  254. if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
  255. return -EPROTO;
  256. data->block[0] = len;
  257. for (i = 0; i < len; i++)
  258. data->block[i + 1] = inb_p(SMBBLKDAT);
  259. }
  260. return 0;
  261. }
  262. static int i801_block_transaction_byte_by_byte(union i2c_smbus_data *data,
  263. char read_write, int command,
  264. int hwpec)
  265. {
  266. int i, len;
  267. int smbcmd;
  268. int status;
  269. int result;
  270. int timeout;
  271. result = i801_check_pre();
  272. if (result < 0)
  273. return result;
  274. len = data->block[0];
  275. if (read_write == I2C_SMBUS_WRITE) {
  276. outb_p(len, SMBHSTDAT0);
  277. outb_p(data->block[1], SMBBLKDAT);
  278. }
  279. for (i = 1; i <= len; i++) {
  280. if (i == len && read_write == I2C_SMBUS_READ) {
  281. if (command == I2C_SMBUS_I2C_BLOCK_DATA)
  282. smbcmd = I801_I2C_BLOCK_LAST;
  283. else
  284. smbcmd = I801_BLOCK_LAST;
  285. } else {
  286. if (command == I2C_SMBUS_I2C_BLOCK_DATA
  287. && read_write == I2C_SMBUS_READ)
  288. smbcmd = I801_I2C_BLOCK_DATA;
  289. else
  290. smbcmd = I801_BLOCK_DATA;
  291. }
  292. outb_p(smbcmd | ENABLE_INT9, SMBHSTCNT);
  293. if (i == 1)
  294. outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT);
  295. /* We will always wait for a fraction of a second! */
  296. timeout = 0;
  297. do {
  298. msleep(1);
  299. status = inb_p(SMBHSTSTS);
  300. } while ((!(status & SMBHSTSTS_BYTE_DONE))
  301. && (timeout++ < MAX_TIMEOUT));
  302. result = i801_check_post(status, timeout > MAX_TIMEOUT);
  303. if (result < 0)
  304. return result;
  305. if (i == 1 && read_write == I2C_SMBUS_READ
  306. && command != I2C_SMBUS_I2C_BLOCK_DATA) {
  307. len = inb_p(SMBHSTDAT0);
  308. if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
  309. dev_err(&I801_dev->dev,
  310. "Illegal SMBus block read size %d\n",
  311. len);
  312. /* Recover */
  313. while (inb_p(SMBHSTSTS) & SMBHSTSTS_HOST_BUSY)
  314. outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS);
  315. outb_p(SMBHSTSTS_INTR, SMBHSTSTS);
  316. return -EPROTO;
  317. }
  318. data->block[0] = len;
  319. }
  320. /* Retrieve/store value in SMBBLKDAT */
  321. if (read_write == I2C_SMBUS_READ)
  322. data->block[i] = inb_p(SMBBLKDAT);
  323. if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
  324. outb_p(data->block[i+1], SMBBLKDAT);
  325. /* signals SMBBLKDAT ready */
  326. outb_p(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR, SMBHSTSTS);
  327. }
  328. return 0;
  329. }
  330. static int i801_set_block_buffer_mode(void)
  331. {
  332. outb_p(inb_p(SMBAUXCTL) | SMBAUXCTL_E32B, SMBAUXCTL);
  333. if ((inb_p(SMBAUXCTL) & SMBAUXCTL_E32B) == 0)
  334. return -EIO;
  335. return 0;
  336. }
  337. /* Block transaction function */
  338. static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
  339. int command, int hwpec)
  340. {
  341. int result = 0;
  342. unsigned char hostc;
  343. if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
  344. if (read_write == I2C_SMBUS_WRITE) {
  345. /* set I2C_EN bit in configuration register */
  346. pci_read_config_byte(I801_dev, SMBHSTCFG, &hostc);
  347. pci_write_config_byte(I801_dev, SMBHSTCFG,
  348. hostc | SMBHSTCFG_I2C_EN);
  349. } else if (!(i801_features & FEATURE_I2C_BLOCK_READ)) {
  350. dev_err(&I801_dev->dev,
  351. "I2C block read is unsupported!\n");
  352. return -EOPNOTSUPP;
  353. }
  354. }
  355. if (read_write == I2C_SMBUS_WRITE
  356. || command == I2C_SMBUS_I2C_BLOCK_DATA) {
  357. if (data->block[0] < 1)
  358. data->block[0] = 1;
  359. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  360. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  361. } else {
  362. data->block[0] = 32; /* max for SMBus block reads */
  363. }
  364. /* Experience has shown that the block buffer can only be used for
  365. SMBus (not I2C) block transactions, even though the datasheet
  366. doesn't mention this limitation. */
  367. if ((i801_features & FEATURE_BLOCK_BUFFER)
  368. && command != I2C_SMBUS_I2C_BLOCK_DATA
  369. && i801_set_block_buffer_mode() == 0)
  370. result = i801_block_transaction_by_block(data, read_write,
  371. hwpec);
  372. else
  373. result = i801_block_transaction_byte_by_byte(data, read_write,
  374. command, hwpec);
  375. if (result == 0 && hwpec)
  376. i801_wait_hwpec();
  377. if (command == I2C_SMBUS_I2C_BLOCK_DATA
  378. && read_write == I2C_SMBUS_WRITE) {
  379. /* restore saved configuration register value */
  380. pci_write_config_byte(I801_dev, SMBHSTCFG, hostc);
  381. }
  382. return result;
  383. }
  384. /* Return negative errno on error. */
  385. static s32 i801_access(struct i2c_adapter *adap, u16 addr,
  386. unsigned short flags, char read_write, u8 command,
  387. int size, union i2c_smbus_data *data)
  388. {
  389. int hwpec;
  390. int block = 0;
  391. int ret, xact = 0;
  392. hwpec = (i801_features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
  393. && size != I2C_SMBUS_QUICK
  394. && size != I2C_SMBUS_I2C_BLOCK_DATA;
  395. switch (size) {
  396. case I2C_SMBUS_QUICK:
  397. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  398. SMBHSTADD);
  399. xact = I801_QUICK;
  400. break;
  401. case I2C_SMBUS_BYTE:
  402. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  403. SMBHSTADD);
  404. if (read_write == I2C_SMBUS_WRITE)
  405. outb_p(command, SMBHSTCMD);
  406. xact = I801_BYTE;
  407. break;
  408. case I2C_SMBUS_BYTE_DATA:
  409. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  410. SMBHSTADD);
  411. outb_p(command, SMBHSTCMD);
  412. if (read_write == I2C_SMBUS_WRITE)
  413. outb_p(data->byte, SMBHSTDAT0);
  414. xact = I801_BYTE_DATA;
  415. break;
  416. case I2C_SMBUS_WORD_DATA:
  417. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  418. SMBHSTADD);
  419. outb_p(command, SMBHSTCMD);
  420. if (read_write == I2C_SMBUS_WRITE) {
  421. outb_p(data->word & 0xff, SMBHSTDAT0);
  422. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  423. }
  424. xact = I801_WORD_DATA;
  425. break;
  426. case I2C_SMBUS_BLOCK_DATA:
  427. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  428. SMBHSTADD);
  429. outb_p(command, SMBHSTCMD);
  430. block = 1;
  431. break;
  432. case I2C_SMBUS_I2C_BLOCK_DATA:
  433. /* NB: page 240 of ICH5 datasheet shows that the R/#W
  434. * bit should be cleared here, even when reading */
  435. outb_p((addr & 0x7f) << 1, SMBHSTADD);
  436. if (read_write == I2C_SMBUS_READ) {
  437. /* NB: page 240 of ICH5 datasheet also shows
  438. * that DATA1 is the cmd field when reading */
  439. outb_p(command, SMBHSTDAT1);
  440. } else
  441. outb_p(command, SMBHSTCMD);
  442. block = 1;
  443. break;
  444. default:
  445. dev_err(&I801_dev->dev, "Unsupported transaction %d\n", size);
  446. return -EOPNOTSUPP;
  447. }
  448. if (hwpec) /* enable/disable hardware PEC */
  449. outb_p(inb_p(SMBAUXCTL) | SMBAUXCTL_CRC, SMBAUXCTL);
  450. else
  451. outb_p(inb_p(SMBAUXCTL) & (~SMBAUXCTL_CRC), SMBAUXCTL);
  452. if (block)
  453. ret = i801_block_transaction(data, read_write, size, hwpec);
  454. else
  455. ret = i801_transaction(xact | ENABLE_INT9);
  456. /* Some BIOSes don't like it when PEC is enabled at reboot or resume
  457. time, so we forcibly disable it after every transaction. Turn off
  458. E32B for the same reason. */
  459. if (hwpec || block)
  460. outb_p(inb_p(SMBAUXCTL) & ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B),
  461. SMBAUXCTL);
  462. if (block)
  463. return ret;
  464. if (ret)
  465. return ret;
  466. if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
  467. return 0;
  468. switch (xact & 0x7f) {
  469. case I801_BYTE: /* Result put in SMBHSTDAT0 */
  470. case I801_BYTE_DATA:
  471. data->byte = inb_p(SMBHSTDAT0);
  472. break;
  473. case I801_WORD_DATA:
  474. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  475. break;
  476. }
  477. return 0;
  478. }
  479. static u32 i801_func(struct i2c_adapter *adapter)
  480. {
  481. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  482. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  483. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
  484. ((i801_features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
  485. ((i801_features & FEATURE_I2C_BLOCK_READ) ?
  486. I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
  487. }
  488. static const struct i2c_algorithm smbus_algorithm = {
  489. .smbus_xfer = i801_access,
  490. .functionality = i801_func,
  491. };
  492. static struct i2c_adapter i801_adapter = {
  493. .owner = THIS_MODULE,
  494. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  495. .algo = &smbus_algorithm,
  496. };
  497. static const struct pci_device_id i801_ids[] = {
  498. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
  499. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
  500. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
  501. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
  502. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
  503. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
  504. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
  505. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
  506. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
  507. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
  508. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
  509. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
  510. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_TOLAPAI_1) },
  511. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
  512. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
  513. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PCH_SMBUS) },
  514. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CPT_SMBUS) },
  515. { 0, }
  516. };
  517. MODULE_DEVICE_TABLE(pci, i801_ids);
  518. #if defined CONFIG_INPUT_APANEL || defined CONFIG_INPUT_APANEL_MODULE
  519. static unsigned char apanel_addr;
  520. /* Scan the system ROM for the signature "FJKEYINF" */
  521. static __init const void __iomem *bios_signature(const void __iomem *bios)
  522. {
  523. ssize_t offset;
  524. const unsigned char signature[] = "FJKEYINF";
  525. for (offset = 0; offset < 0x10000; offset += 0x10) {
  526. if (check_signature(bios + offset, signature,
  527. sizeof(signature)-1))
  528. return bios + offset;
  529. }
  530. return NULL;
  531. }
  532. static void __init input_apanel_init(void)
  533. {
  534. void __iomem *bios;
  535. const void __iomem *p;
  536. bios = ioremap(0xF0000, 0x10000); /* Can't fail */
  537. p = bios_signature(bios);
  538. if (p) {
  539. /* just use the first address */
  540. apanel_addr = readb(p + 8 + 3) >> 1;
  541. }
  542. iounmap(bios);
  543. }
  544. #else
  545. static void __init input_apanel_init(void) {}
  546. #endif
  547. #if defined CONFIG_SENSORS_FSCHMD || defined CONFIG_SENSORS_FSCHMD_MODULE
  548. struct dmi_onboard_device_info {
  549. const char *name;
  550. u8 type;
  551. unsigned short i2c_addr;
  552. const char *i2c_type;
  553. };
  554. static struct dmi_onboard_device_info __devinitdata dmi_devices[] = {
  555. { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
  556. { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
  557. { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
  558. };
  559. static void __devinit dmi_check_onboard_device(u8 type, const char *name,
  560. struct i2c_adapter *adap)
  561. {
  562. int i;
  563. struct i2c_board_info info;
  564. for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
  565. /* & ~0x80, ignore enabled/disabled bit */
  566. if ((type & ~0x80) != dmi_devices[i].type)
  567. continue;
  568. if (strcasecmp(name, dmi_devices[i].name))
  569. continue;
  570. memset(&info, 0, sizeof(struct i2c_board_info));
  571. info.addr = dmi_devices[i].i2c_addr;
  572. strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
  573. i2c_new_device(adap, &info);
  574. break;
  575. }
  576. }
  577. /* We use our own function to check for onboard devices instead of
  578. dmi_find_device() as some buggy BIOS's have the devices we are interested
  579. in marked as disabled */
  580. static void __devinit dmi_check_onboard_devices(const struct dmi_header *dm,
  581. void *adap)
  582. {
  583. int i, count;
  584. if (dm->type != 10)
  585. return;
  586. count = (dm->length - sizeof(struct dmi_header)) / 2;
  587. for (i = 0; i < count; i++) {
  588. const u8 *d = (char *)(dm + 1) + (i * 2);
  589. const char *name = ((char *) dm) + dm->length;
  590. u8 type = d[0];
  591. u8 s = d[1];
  592. if (!s)
  593. continue;
  594. s--;
  595. while (s > 0 && name[0]) {
  596. name += strlen(name) + 1;
  597. s--;
  598. }
  599. if (name[0] == 0) /* Bogus string reference */
  600. continue;
  601. dmi_check_onboard_device(type, name, adap);
  602. }
  603. }
  604. #endif
  605. static int __devinit i801_probe(struct pci_dev *dev,
  606. const struct pci_device_id *id)
  607. {
  608. unsigned char temp;
  609. int err, i;
  610. I801_dev = dev;
  611. i801_features = 0;
  612. switch (dev->device) {
  613. default:
  614. i801_features |= FEATURE_I2C_BLOCK_READ;
  615. /* fall through */
  616. case PCI_DEVICE_ID_INTEL_82801DB_3:
  617. i801_features |= FEATURE_SMBUS_PEC;
  618. i801_features |= FEATURE_BLOCK_BUFFER;
  619. /* fall through */
  620. case PCI_DEVICE_ID_INTEL_82801CA_3:
  621. case PCI_DEVICE_ID_INTEL_82801BA_2:
  622. case PCI_DEVICE_ID_INTEL_82801AB_3:
  623. case PCI_DEVICE_ID_INTEL_82801AA_3:
  624. break;
  625. }
  626. /* Disable features on user request */
  627. for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
  628. if (i801_features & disable_features & (1 << i))
  629. dev_notice(&dev->dev, "%s disabled by user\n",
  630. i801_feature_names[i]);
  631. }
  632. i801_features &= ~disable_features;
  633. err = pci_enable_device(dev);
  634. if (err) {
  635. dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
  636. err);
  637. goto exit;
  638. }
  639. /* Determine the address of the SMBus area */
  640. i801_smba = pci_resource_start(dev, SMBBAR);
  641. if (!i801_smba) {
  642. dev_err(&dev->dev, "SMBus base address uninitialized, "
  643. "upgrade BIOS\n");
  644. err = -ENODEV;
  645. goto exit;
  646. }
  647. err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
  648. if (err) {
  649. err = -ENODEV;
  650. goto exit;
  651. }
  652. err = pci_request_region(dev, SMBBAR, i801_driver.name);
  653. if (err) {
  654. dev_err(&dev->dev, "Failed to request SMBus region "
  655. "0x%lx-0x%Lx\n", i801_smba,
  656. (unsigned long long)pci_resource_end(dev, SMBBAR));
  657. goto exit;
  658. }
  659. pci_read_config_byte(I801_dev, SMBHSTCFG, &temp);
  660. i801_original_hstcfg = temp;
  661. temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
  662. if (!(temp & SMBHSTCFG_HST_EN)) {
  663. dev_info(&dev->dev, "Enabling SMBus device\n");
  664. temp |= SMBHSTCFG_HST_EN;
  665. }
  666. pci_write_config_byte(I801_dev, SMBHSTCFG, temp);
  667. if (temp & SMBHSTCFG_SMB_SMI_EN)
  668. dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
  669. else
  670. dev_dbg(&dev->dev, "SMBus using PCI Interrupt\n");
  671. /* Clear special mode bits */
  672. if (i801_features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
  673. outb_p(inb_p(SMBAUXCTL) & ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B),
  674. SMBAUXCTL);
  675. /* set up the sysfs linkage to our parent device */
  676. i801_adapter.dev.parent = &dev->dev;
  677. /* Retry up to 3 times on lost arbitration */
  678. i801_adapter.retries = 3;
  679. snprintf(i801_adapter.name, sizeof(i801_adapter.name),
  680. "SMBus I801 adapter at %04lx", i801_smba);
  681. err = i2c_add_adapter(&i801_adapter);
  682. if (err) {
  683. dev_err(&dev->dev, "Failed to add SMBus adapter\n");
  684. goto exit_release;
  685. }
  686. /* Register optional slaves */
  687. #if defined CONFIG_INPUT_APANEL || defined CONFIG_INPUT_APANEL_MODULE
  688. if (apanel_addr) {
  689. struct i2c_board_info info;
  690. memset(&info, 0, sizeof(struct i2c_board_info));
  691. info.addr = apanel_addr;
  692. strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
  693. i2c_new_device(&i801_adapter, &info);
  694. }
  695. #endif
  696. #if defined CONFIG_SENSORS_FSCHMD || defined CONFIG_SENSORS_FSCHMD_MODULE
  697. if (dmi_name_in_vendors("FUJITSU"))
  698. dmi_walk(dmi_check_onboard_devices, &i801_adapter);
  699. #endif
  700. return 0;
  701. exit_release:
  702. pci_release_region(dev, SMBBAR);
  703. exit:
  704. return err;
  705. }
  706. static void __devexit i801_remove(struct pci_dev *dev)
  707. {
  708. i2c_del_adapter(&i801_adapter);
  709. pci_write_config_byte(I801_dev, SMBHSTCFG, i801_original_hstcfg);
  710. pci_release_region(dev, SMBBAR);
  711. /*
  712. * do not call pci_disable_device(dev) since it can cause hard hangs on
  713. * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
  714. */
  715. }
  716. #ifdef CONFIG_PM
  717. static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
  718. {
  719. pci_save_state(dev);
  720. pci_write_config_byte(dev, SMBHSTCFG, i801_original_hstcfg);
  721. pci_set_power_state(dev, pci_choose_state(dev, mesg));
  722. return 0;
  723. }
  724. static int i801_resume(struct pci_dev *dev)
  725. {
  726. pci_set_power_state(dev, PCI_D0);
  727. pci_restore_state(dev);
  728. return pci_enable_device(dev);
  729. }
  730. #else
  731. #define i801_suspend NULL
  732. #define i801_resume NULL
  733. #endif
  734. static struct pci_driver i801_driver = {
  735. .name = "i801_smbus",
  736. .id_table = i801_ids,
  737. .probe = i801_probe,
  738. .remove = __devexit_p(i801_remove),
  739. .suspend = i801_suspend,
  740. .resume = i801_resume,
  741. };
  742. static int __init i2c_i801_init(void)
  743. {
  744. input_apanel_init();
  745. return pci_register_driver(&i801_driver);
  746. }
  747. static void __exit i2c_i801_exit(void)
  748. {
  749. pci_unregister_driver(&i801_driver);
  750. }
  751. MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, "
  752. "Jean Delvare <khali@linux-fr.org>");
  753. MODULE_DESCRIPTION("I801 SMBus driver");
  754. MODULE_LICENSE("GPL");
  755. module_init(i2c_i801_init);
  756. module_exit(i2c_i801_exit);