i2c-i801.c 26 KB

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