i2c-i801.c 28 KB

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