i2c-i801.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 - 2014 Jean Delvare <jdelvare@suse.de>
  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. */
  17. /*
  18. * Supports the following Intel I/O Controller Hubs (ICH):
  19. *
  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. * EP80579 (Tolapai) 0x5032 32 hard yes yes yes
  37. * ICH10 0x3a30 32 hard yes yes yes
  38. * ICH10 0x3a60 32 hard yes yes yes
  39. * 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
  40. * 6 Series (PCH) 0x1c22 32 hard yes yes yes
  41. * Patsburg (PCH) 0x1d22 32 hard yes yes yes
  42. * Patsburg (PCH) IDF 0x1d70 32 hard yes yes yes
  43. * Patsburg (PCH) IDF 0x1d71 32 hard yes yes yes
  44. * Patsburg (PCH) IDF 0x1d72 32 hard yes yes yes
  45. * DH89xxCC (PCH) 0x2330 32 hard yes yes yes
  46. * Panther Point (PCH) 0x1e22 32 hard yes yes yes
  47. * Lynx Point (PCH) 0x8c22 32 hard yes yes yes
  48. * Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
  49. * Avoton (SOC) 0x1f3c 32 hard yes yes yes
  50. * Wellsburg (PCH) 0x8d22 32 hard yes yes yes
  51. * Wellsburg (PCH) MS 0x8d7d 32 hard yes yes yes
  52. * Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes
  53. * Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes
  54. * Coleto Creek (PCH) 0x23b0 32 hard yes yes yes
  55. * Wildcat Point (PCH) 0x8ca2 32 hard yes yes yes
  56. * Wildcat Point-LP (PCH) 0x9ca2 32 hard yes yes yes
  57. * BayTrail (SOC) 0x0f12 32 hard yes yes yes
  58. * Sunrise Point-H (PCH) 0xa123 32 hard yes yes yes
  59. * Sunrise Point-LP (PCH) 0x9d23 32 hard yes yes yes
  60. *
  61. * Features supported by this driver:
  62. * Software PEC no
  63. * Hardware PEC yes
  64. * Block buffer yes
  65. * Block process call transaction no
  66. * I2C block read transaction yes (doesn't use the block buffer)
  67. * Slave mode no
  68. * Interrupt processing yes
  69. *
  70. * See the file Documentation/i2c/busses/i2c-i801 for details.
  71. */
  72. #include <linux/interrupt.h>
  73. #include <linux/module.h>
  74. #include <linux/pci.h>
  75. #include <linux/kernel.h>
  76. #include <linux/stddef.h>
  77. #include <linux/delay.h>
  78. #include <linux/ioport.h>
  79. #include <linux/init.h>
  80. #include <linux/i2c.h>
  81. #include <linux/acpi.h>
  82. #include <linux/io.h>
  83. #include <linux/dmi.h>
  84. #include <linux/slab.h>
  85. #include <linux/wait.h>
  86. #include <linux/err.h>
  87. #if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
  88. defined CONFIG_DMI
  89. #include <linux/gpio.h>
  90. #include <linux/i2c-mux-gpio.h>
  91. #include <linux/platform_device.h>
  92. #endif
  93. /* I801 SMBus address offsets */
  94. #define SMBHSTSTS(p) (0 + (p)->smba)
  95. #define SMBHSTCNT(p) (2 + (p)->smba)
  96. #define SMBHSTCMD(p) (3 + (p)->smba)
  97. #define SMBHSTADD(p) (4 + (p)->smba)
  98. #define SMBHSTDAT0(p) (5 + (p)->smba)
  99. #define SMBHSTDAT1(p) (6 + (p)->smba)
  100. #define SMBBLKDAT(p) (7 + (p)->smba)
  101. #define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
  102. #define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
  103. #define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
  104. /* PCI Address Constants */
  105. #define SMBBAR 4
  106. #define SMBPCICTL 0x004
  107. #define SMBPCISTS 0x006
  108. #define SMBHSTCFG 0x040
  109. /* Host status bits for SMBPCISTS */
  110. #define SMBPCISTS_INTS 0x08
  111. /* Control bits for SMBPCICTL */
  112. #define SMBPCICTL_INTDIS 0x0400
  113. /* Host configuration bits for SMBHSTCFG */
  114. #define SMBHSTCFG_HST_EN 1
  115. #define SMBHSTCFG_SMB_SMI_EN 2
  116. #define SMBHSTCFG_I2C_EN 4
  117. /* Auxiliary control register bits, ICH4+ only */
  118. #define SMBAUXCTL_CRC 1
  119. #define SMBAUXCTL_E32B 2
  120. /* Other settings */
  121. #define MAX_RETRIES 400
  122. /* I801 command constants */
  123. #define I801_QUICK 0x00
  124. #define I801_BYTE 0x04
  125. #define I801_BYTE_DATA 0x08
  126. #define I801_WORD_DATA 0x0C
  127. #define I801_PROC_CALL 0x10 /* unimplemented */
  128. #define I801_BLOCK_DATA 0x14
  129. #define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
  130. /* I801 Host Control register bits */
  131. #define SMBHSTCNT_INTREN 0x01
  132. #define SMBHSTCNT_KILL 0x02
  133. #define SMBHSTCNT_LAST_BYTE 0x20
  134. #define SMBHSTCNT_START 0x40
  135. #define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
  136. /* I801 Hosts Status register bits */
  137. #define SMBHSTSTS_BYTE_DONE 0x80
  138. #define SMBHSTSTS_INUSE_STS 0x40
  139. #define SMBHSTSTS_SMBALERT_STS 0x20
  140. #define SMBHSTSTS_FAILED 0x10
  141. #define SMBHSTSTS_BUS_ERR 0x08
  142. #define SMBHSTSTS_DEV_ERR 0x04
  143. #define SMBHSTSTS_INTR 0x02
  144. #define SMBHSTSTS_HOST_BUSY 0x01
  145. #define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
  146. SMBHSTSTS_DEV_ERR)
  147. #define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
  148. STATUS_ERROR_FLAGS)
  149. /* Older devices have their ID defined in <linux/pci_ids.h> */
  150. #define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
  151. #define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
  152. #define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
  153. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
  154. /* Patsburg also has three 'Integrated Device Function' SMBus controllers */
  155. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
  156. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
  157. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
  158. #define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
  159. #define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
  160. #define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
  161. #define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
  162. #define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
  163. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
  164. #define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
  165. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
  166. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
  167. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
  168. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
  169. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
  170. #define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
  171. #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
  172. #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23
  173. struct i801_mux_config {
  174. char *gpio_chip;
  175. unsigned values[3];
  176. int n_values;
  177. unsigned classes[3];
  178. unsigned gpios[2]; /* Relative to gpio_chip->base */
  179. int n_gpios;
  180. };
  181. struct i801_priv {
  182. struct i2c_adapter adapter;
  183. unsigned long smba;
  184. unsigned char original_hstcfg;
  185. struct pci_dev *pci_dev;
  186. unsigned int features;
  187. /* isr processing */
  188. wait_queue_head_t waitq;
  189. u8 status;
  190. /* Command state used by isr for byte-by-byte block transactions */
  191. u8 cmd;
  192. bool is_read;
  193. int count;
  194. int len;
  195. u8 *data;
  196. #if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
  197. defined CONFIG_DMI
  198. const struct i801_mux_config *mux_drvdata;
  199. struct platform_device *mux_pdev;
  200. #endif
  201. };
  202. #define FEATURE_SMBUS_PEC (1 << 0)
  203. #define FEATURE_BLOCK_BUFFER (1 << 1)
  204. #define FEATURE_BLOCK_PROC (1 << 2)
  205. #define FEATURE_I2C_BLOCK_READ (1 << 3)
  206. #define FEATURE_IRQ (1 << 4)
  207. /* Not really a feature, but it's convenient to handle it as such */
  208. #define FEATURE_IDF (1 << 15)
  209. static const char *i801_feature_names[] = {
  210. "SMBus PEC",
  211. "Block buffer",
  212. "Block process call",
  213. "I2C block read",
  214. "Interrupt",
  215. };
  216. static unsigned int disable_features;
  217. module_param(disable_features, uint, S_IRUGO | S_IWUSR);
  218. MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
  219. "\t\t 0x01 disable SMBus PEC\n"
  220. "\t\t 0x02 disable the block buffer\n"
  221. "\t\t 0x08 disable the I2C block read functionality\n"
  222. "\t\t 0x10 don't use interrupts ");
  223. /* Make sure the SMBus host is ready to start transmitting.
  224. Return 0 if it is, -EBUSY if it is not. */
  225. static int i801_check_pre(struct i801_priv *priv)
  226. {
  227. int status;
  228. status = inb_p(SMBHSTSTS(priv));
  229. if (status & SMBHSTSTS_HOST_BUSY) {
  230. dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
  231. return -EBUSY;
  232. }
  233. status &= STATUS_FLAGS;
  234. if (status) {
  235. dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
  236. status);
  237. outb_p(status, SMBHSTSTS(priv));
  238. status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
  239. if (status) {
  240. dev_err(&priv->pci_dev->dev,
  241. "Failed clearing status flags (%02x)\n",
  242. status);
  243. return -EBUSY;
  244. }
  245. }
  246. return 0;
  247. }
  248. /*
  249. * Convert the status register to an error code, and clear it.
  250. * Note that status only contains the bits we want to clear, not the
  251. * actual register value.
  252. */
  253. static int i801_check_post(struct i801_priv *priv, int status)
  254. {
  255. int result = 0;
  256. /*
  257. * If the SMBus is still busy, we give up
  258. * Note: This timeout condition only happens when using polling
  259. * transactions. For interrupt operation, NAK/timeout is indicated by
  260. * DEV_ERR.
  261. */
  262. if (unlikely(status < 0)) {
  263. dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
  264. /* try to stop the current command */
  265. dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
  266. outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
  267. SMBHSTCNT(priv));
  268. usleep_range(1000, 2000);
  269. outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
  270. SMBHSTCNT(priv));
  271. /* Check if it worked */
  272. status = inb_p(SMBHSTSTS(priv));
  273. if ((status & SMBHSTSTS_HOST_BUSY) ||
  274. !(status & SMBHSTSTS_FAILED))
  275. dev_err(&priv->pci_dev->dev,
  276. "Failed terminating the transaction\n");
  277. outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
  278. return -ETIMEDOUT;
  279. }
  280. if (status & SMBHSTSTS_FAILED) {
  281. result = -EIO;
  282. dev_err(&priv->pci_dev->dev, "Transaction failed\n");
  283. }
  284. if (status & SMBHSTSTS_DEV_ERR) {
  285. result = -ENXIO;
  286. dev_dbg(&priv->pci_dev->dev, "No response\n");
  287. }
  288. if (status & SMBHSTSTS_BUS_ERR) {
  289. result = -EAGAIN;
  290. dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
  291. }
  292. /* Clear status flags except BYTE_DONE, to be cleared by caller */
  293. outb_p(status, SMBHSTSTS(priv));
  294. return result;
  295. }
  296. /* Wait for BUSY being cleared and either INTR or an error flag being set */
  297. static int i801_wait_intr(struct i801_priv *priv)
  298. {
  299. int timeout = 0;
  300. int status;
  301. /* We will always wait for a fraction of a second! */
  302. do {
  303. usleep_range(250, 500);
  304. status = inb_p(SMBHSTSTS(priv));
  305. } while (((status & SMBHSTSTS_HOST_BUSY) ||
  306. !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
  307. (timeout++ < MAX_RETRIES));
  308. if (timeout > MAX_RETRIES) {
  309. dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
  310. return -ETIMEDOUT;
  311. }
  312. return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
  313. }
  314. /* Wait for either BYTE_DONE or an error flag being set */
  315. static int i801_wait_byte_done(struct i801_priv *priv)
  316. {
  317. int timeout = 0;
  318. int status;
  319. /* We will always wait for a fraction of a second! */
  320. do {
  321. usleep_range(250, 500);
  322. status = inb_p(SMBHSTSTS(priv));
  323. } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
  324. (timeout++ < MAX_RETRIES));
  325. if (timeout > MAX_RETRIES) {
  326. dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
  327. return -ETIMEDOUT;
  328. }
  329. return status & STATUS_ERROR_FLAGS;
  330. }
  331. static int i801_transaction(struct i801_priv *priv, int xact)
  332. {
  333. int status;
  334. int result;
  335. const struct i2c_adapter *adap = &priv->adapter;
  336. result = i801_check_pre(priv);
  337. if (result < 0)
  338. return result;
  339. if (priv->features & FEATURE_IRQ) {
  340. outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
  341. SMBHSTCNT(priv));
  342. result = wait_event_timeout(priv->waitq,
  343. (status = priv->status),
  344. adap->timeout);
  345. if (!result) {
  346. status = -ETIMEDOUT;
  347. dev_warn(&priv->pci_dev->dev,
  348. "Timeout waiting for interrupt!\n");
  349. }
  350. priv->status = 0;
  351. return i801_check_post(priv, status);
  352. }
  353. /* the current contents of SMBHSTCNT can be overwritten, since PEC,
  354. * SMBSCMD are passed in xact */
  355. outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
  356. status = i801_wait_intr(priv);
  357. return i801_check_post(priv, status);
  358. }
  359. static int i801_block_transaction_by_block(struct i801_priv *priv,
  360. union i2c_smbus_data *data,
  361. char read_write, int hwpec)
  362. {
  363. int i, len;
  364. int status;
  365. inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
  366. /* Use 32-byte buffer to process this transaction */
  367. if (read_write == I2C_SMBUS_WRITE) {
  368. len = data->block[0];
  369. outb_p(len, SMBHSTDAT0(priv));
  370. for (i = 0; i < len; i++)
  371. outb_p(data->block[i+1], SMBBLKDAT(priv));
  372. }
  373. status = i801_transaction(priv, I801_BLOCK_DATA |
  374. (hwpec ? SMBHSTCNT_PEC_EN : 0));
  375. if (status)
  376. return status;
  377. if (read_write == I2C_SMBUS_READ) {
  378. len = inb_p(SMBHSTDAT0(priv));
  379. if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
  380. return -EPROTO;
  381. data->block[0] = len;
  382. for (i = 0; i < len; i++)
  383. data->block[i + 1] = inb_p(SMBBLKDAT(priv));
  384. }
  385. return 0;
  386. }
  387. static void i801_isr_byte_done(struct i801_priv *priv)
  388. {
  389. if (priv->is_read) {
  390. /* For SMBus block reads, length is received with first byte */
  391. if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
  392. (priv->count == 0)) {
  393. priv->len = inb_p(SMBHSTDAT0(priv));
  394. if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
  395. dev_err(&priv->pci_dev->dev,
  396. "Illegal SMBus block read size %d\n",
  397. priv->len);
  398. /* FIXME: Recover */
  399. priv->len = I2C_SMBUS_BLOCK_MAX;
  400. } else {
  401. dev_dbg(&priv->pci_dev->dev,
  402. "SMBus block read size is %d\n",
  403. priv->len);
  404. }
  405. priv->data[-1] = priv->len;
  406. }
  407. /* Read next byte */
  408. if (priv->count < priv->len)
  409. priv->data[priv->count++] = inb(SMBBLKDAT(priv));
  410. else
  411. dev_dbg(&priv->pci_dev->dev,
  412. "Discarding extra byte on block read\n");
  413. /* Set LAST_BYTE for last byte of read transaction */
  414. if (priv->count == priv->len - 1)
  415. outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
  416. SMBHSTCNT(priv));
  417. } else if (priv->count < priv->len - 1) {
  418. /* Write next byte, except for IRQ after last byte */
  419. outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
  420. }
  421. /* Clear BYTE_DONE to continue with next byte */
  422. outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
  423. }
  424. /*
  425. * There are two kinds of interrupts:
  426. *
  427. * 1) i801 signals transaction completion with one of these interrupts:
  428. * INTR - Success
  429. * DEV_ERR - Invalid command, NAK or communication timeout
  430. * BUS_ERR - SMI# transaction collision
  431. * FAILED - transaction was canceled due to a KILL request
  432. * When any of these occur, update ->status and wake up the waitq.
  433. * ->status must be cleared before kicking off the next transaction.
  434. *
  435. * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
  436. * occurs for each byte of a byte-by-byte to prepare the next byte.
  437. */
  438. static irqreturn_t i801_isr(int irq, void *dev_id)
  439. {
  440. struct i801_priv *priv = dev_id;
  441. u16 pcists;
  442. u8 status;
  443. /* Confirm this is our interrupt */
  444. pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
  445. if (!(pcists & SMBPCISTS_INTS))
  446. return IRQ_NONE;
  447. status = inb_p(SMBHSTSTS(priv));
  448. if (status & SMBHSTSTS_BYTE_DONE)
  449. i801_isr_byte_done(priv);
  450. /*
  451. * Clear irq sources and report transaction result.
  452. * ->status must be cleared before the next transaction is started.
  453. */
  454. status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
  455. if (status) {
  456. outb_p(status, SMBHSTSTS(priv));
  457. priv->status |= status;
  458. wake_up(&priv->waitq);
  459. }
  460. return IRQ_HANDLED;
  461. }
  462. /*
  463. * For "byte-by-byte" block transactions:
  464. * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
  465. * I2C read uses cmd=I801_I2C_BLOCK_DATA
  466. */
  467. static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
  468. union i2c_smbus_data *data,
  469. char read_write, int command,
  470. int hwpec)
  471. {
  472. int i, len;
  473. int smbcmd;
  474. int status;
  475. int result;
  476. const struct i2c_adapter *adap = &priv->adapter;
  477. result = i801_check_pre(priv);
  478. if (result < 0)
  479. return result;
  480. len = data->block[0];
  481. if (read_write == I2C_SMBUS_WRITE) {
  482. outb_p(len, SMBHSTDAT0(priv));
  483. outb_p(data->block[1], SMBBLKDAT(priv));
  484. }
  485. if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
  486. read_write == I2C_SMBUS_READ)
  487. smbcmd = I801_I2C_BLOCK_DATA;
  488. else
  489. smbcmd = I801_BLOCK_DATA;
  490. if (priv->features & FEATURE_IRQ) {
  491. priv->is_read = (read_write == I2C_SMBUS_READ);
  492. if (len == 1 && priv->is_read)
  493. smbcmd |= SMBHSTCNT_LAST_BYTE;
  494. priv->cmd = smbcmd | SMBHSTCNT_INTREN;
  495. priv->len = len;
  496. priv->count = 0;
  497. priv->data = &data->block[1];
  498. outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
  499. result = wait_event_timeout(priv->waitq,
  500. (status = priv->status),
  501. adap->timeout);
  502. if (!result) {
  503. status = -ETIMEDOUT;
  504. dev_warn(&priv->pci_dev->dev,
  505. "Timeout waiting for interrupt!\n");
  506. }
  507. priv->status = 0;
  508. return i801_check_post(priv, status);
  509. }
  510. for (i = 1; i <= len; i++) {
  511. if (i == len && read_write == I2C_SMBUS_READ)
  512. smbcmd |= SMBHSTCNT_LAST_BYTE;
  513. outb_p(smbcmd, SMBHSTCNT(priv));
  514. if (i == 1)
  515. outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
  516. SMBHSTCNT(priv));
  517. status = i801_wait_byte_done(priv);
  518. if (status)
  519. goto exit;
  520. if (i == 1 && read_write == I2C_SMBUS_READ
  521. && command != I2C_SMBUS_I2C_BLOCK_DATA) {
  522. len = inb_p(SMBHSTDAT0(priv));
  523. if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
  524. dev_err(&priv->pci_dev->dev,
  525. "Illegal SMBus block read size %d\n",
  526. len);
  527. /* Recover */
  528. while (inb_p(SMBHSTSTS(priv)) &
  529. SMBHSTSTS_HOST_BUSY)
  530. outb_p(SMBHSTSTS_BYTE_DONE,
  531. SMBHSTSTS(priv));
  532. outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
  533. return -EPROTO;
  534. }
  535. data->block[0] = len;
  536. }
  537. /* Retrieve/store value in SMBBLKDAT */
  538. if (read_write == I2C_SMBUS_READ)
  539. data->block[i] = inb_p(SMBBLKDAT(priv));
  540. if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
  541. outb_p(data->block[i+1], SMBBLKDAT(priv));
  542. /* signals SMBBLKDAT ready */
  543. outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
  544. }
  545. status = i801_wait_intr(priv);
  546. exit:
  547. return i801_check_post(priv, status);
  548. }
  549. static int i801_set_block_buffer_mode(struct i801_priv *priv)
  550. {
  551. outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
  552. if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
  553. return -EIO;
  554. return 0;
  555. }
  556. /* Block transaction function */
  557. static int i801_block_transaction(struct i801_priv *priv,
  558. union i2c_smbus_data *data, char read_write,
  559. int command, int hwpec)
  560. {
  561. int result = 0;
  562. unsigned char hostc;
  563. if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
  564. if (read_write == I2C_SMBUS_WRITE) {
  565. /* set I2C_EN bit in configuration register */
  566. pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
  567. pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
  568. hostc | SMBHSTCFG_I2C_EN);
  569. } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
  570. dev_err(&priv->pci_dev->dev,
  571. "I2C block read is unsupported!\n");
  572. return -EOPNOTSUPP;
  573. }
  574. }
  575. if (read_write == I2C_SMBUS_WRITE
  576. || command == I2C_SMBUS_I2C_BLOCK_DATA) {
  577. if (data->block[0] < 1)
  578. data->block[0] = 1;
  579. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  580. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  581. } else {
  582. data->block[0] = 32; /* max for SMBus block reads */
  583. }
  584. /* Experience has shown that the block buffer can only be used for
  585. SMBus (not I2C) block transactions, even though the datasheet
  586. doesn't mention this limitation. */
  587. if ((priv->features & FEATURE_BLOCK_BUFFER)
  588. && command != I2C_SMBUS_I2C_BLOCK_DATA
  589. && i801_set_block_buffer_mode(priv) == 0)
  590. result = i801_block_transaction_by_block(priv, data,
  591. read_write, hwpec);
  592. else
  593. result = i801_block_transaction_byte_by_byte(priv, data,
  594. read_write,
  595. command, hwpec);
  596. if (command == I2C_SMBUS_I2C_BLOCK_DATA
  597. && read_write == I2C_SMBUS_WRITE) {
  598. /* restore saved configuration register value */
  599. pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
  600. }
  601. return result;
  602. }
  603. /* Return negative errno on error. */
  604. static s32 i801_access(struct i2c_adapter *adap, u16 addr,
  605. unsigned short flags, char read_write, u8 command,
  606. int size, union i2c_smbus_data *data)
  607. {
  608. int hwpec;
  609. int block = 0;
  610. int ret, xact = 0;
  611. struct i801_priv *priv = i2c_get_adapdata(adap);
  612. hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
  613. && size != I2C_SMBUS_QUICK
  614. && size != I2C_SMBUS_I2C_BLOCK_DATA;
  615. switch (size) {
  616. case I2C_SMBUS_QUICK:
  617. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  618. SMBHSTADD(priv));
  619. xact = I801_QUICK;
  620. break;
  621. case I2C_SMBUS_BYTE:
  622. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  623. SMBHSTADD(priv));
  624. if (read_write == I2C_SMBUS_WRITE)
  625. outb_p(command, SMBHSTCMD(priv));
  626. xact = I801_BYTE;
  627. break;
  628. case I2C_SMBUS_BYTE_DATA:
  629. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  630. SMBHSTADD(priv));
  631. outb_p(command, SMBHSTCMD(priv));
  632. if (read_write == I2C_SMBUS_WRITE)
  633. outb_p(data->byte, SMBHSTDAT0(priv));
  634. xact = I801_BYTE_DATA;
  635. break;
  636. case I2C_SMBUS_WORD_DATA:
  637. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  638. SMBHSTADD(priv));
  639. outb_p(command, SMBHSTCMD(priv));
  640. if (read_write == I2C_SMBUS_WRITE) {
  641. outb_p(data->word & 0xff, SMBHSTDAT0(priv));
  642. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
  643. }
  644. xact = I801_WORD_DATA;
  645. break;
  646. case I2C_SMBUS_BLOCK_DATA:
  647. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  648. SMBHSTADD(priv));
  649. outb_p(command, SMBHSTCMD(priv));
  650. block = 1;
  651. break;
  652. case I2C_SMBUS_I2C_BLOCK_DATA:
  653. /* NB: page 240 of ICH5 datasheet shows that the R/#W
  654. * bit should be cleared here, even when reading */
  655. outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
  656. if (read_write == I2C_SMBUS_READ) {
  657. /* NB: page 240 of ICH5 datasheet also shows
  658. * that DATA1 is the cmd field when reading */
  659. outb_p(command, SMBHSTDAT1(priv));
  660. } else
  661. outb_p(command, SMBHSTCMD(priv));
  662. block = 1;
  663. break;
  664. default:
  665. dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
  666. size);
  667. return -EOPNOTSUPP;
  668. }
  669. if (hwpec) /* enable/disable hardware PEC */
  670. outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
  671. else
  672. outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
  673. SMBAUXCTL(priv));
  674. if (block)
  675. ret = i801_block_transaction(priv, data, read_write, size,
  676. hwpec);
  677. else
  678. ret = i801_transaction(priv, xact);
  679. /* Some BIOSes don't like it when PEC is enabled at reboot or resume
  680. time, so we forcibly disable it after every transaction. Turn off
  681. E32B for the same reason. */
  682. if (hwpec || block)
  683. outb_p(inb_p(SMBAUXCTL(priv)) &
  684. ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
  685. if (block)
  686. return ret;
  687. if (ret)
  688. return ret;
  689. if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
  690. return 0;
  691. switch (xact & 0x7f) {
  692. case I801_BYTE: /* Result put in SMBHSTDAT0 */
  693. case I801_BYTE_DATA:
  694. data->byte = inb_p(SMBHSTDAT0(priv));
  695. break;
  696. case I801_WORD_DATA:
  697. data->word = inb_p(SMBHSTDAT0(priv)) +
  698. (inb_p(SMBHSTDAT1(priv)) << 8);
  699. break;
  700. }
  701. return 0;
  702. }
  703. static u32 i801_func(struct i2c_adapter *adapter)
  704. {
  705. struct i801_priv *priv = i2c_get_adapdata(adapter);
  706. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  707. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  708. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
  709. ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
  710. ((priv->features & FEATURE_I2C_BLOCK_READ) ?
  711. I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
  712. }
  713. static const struct i2c_algorithm smbus_algorithm = {
  714. .smbus_xfer = i801_access,
  715. .functionality = i801_func,
  716. };
  717. static const struct pci_device_id i801_ids[] = {
  718. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
  719. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
  720. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
  721. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
  722. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
  723. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
  724. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
  725. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
  726. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
  727. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
  728. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
  729. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
  730. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
  731. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
  732. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
  733. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
  734. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
  735. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
  736. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
  737. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
  738. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
  739. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
  740. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
  741. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
  742. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
  743. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
  744. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
  745. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
  746. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
  747. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
  748. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
  749. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
  750. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
  751. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
  752. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
  753. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS) },
  754. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS) },
  755. { 0, }
  756. };
  757. MODULE_DEVICE_TABLE(pci, i801_ids);
  758. #if defined CONFIG_X86 && defined CONFIG_DMI
  759. static unsigned char apanel_addr;
  760. /* Scan the system ROM for the signature "FJKEYINF" */
  761. static __init const void __iomem *bios_signature(const void __iomem *bios)
  762. {
  763. ssize_t offset;
  764. const unsigned char signature[] = "FJKEYINF";
  765. for (offset = 0; offset < 0x10000; offset += 0x10) {
  766. if (check_signature(bios + offset, signature,
  767. sizeof(signature)-1))
  768. return bios + offset;
  769. }
  770. return NULL;
  771. }
  772. static void __init input_apanel_init(void)
  773. {
  774. void __iomem *bios;
  775. const void __iomem *p;
  776. bios = ioremap(0xF0000, 0x10000); /* Can't fail */
  777. p = bios_signature(bios);
  778. if (p) {
  779. /* just use the first address */
  780. apanel_addr = readb(p + 8 + 3) >> 1;
  781. }
  782. iounmap(bios);
  783. }
  784. struct dmi_onboard_device_info {
  785. const char *name;
  786. u8 type;
  787. unsigned short i2c_addr;
  788. const char *i2c_type;
  789. };
  790. static const struct dmi_onboard_device_info dmi_devices[] = {
  791. { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
  792. { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
  793. { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
  794. };
  795. static void dmi_check_onboard_device(u8 type, const char *name,
  796. struct i2c_adapter *adap)
  797. {
  798. int i;
  799. struct i2c_board_info info;
  800. for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
  801. /* & ~0x80, ignore enabled/disabled bit */
  802. if ((type & ~0x80) != dmi_devices[i].type)
  803. continue;
  804. if (strcasecmp(name, dmi_devices[i].name))
  805. continue;
  806. memset(&info, 0, sizeof(struct i2c_board_info));
  807. info.addr = dmi_devices[i].i2c_addr;
  808. strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
  809. i2c_new_device(adap, &info);
  810. break;
  811. }
  812. }
  813. /* We use our own function to check for onboard devices instead of
  814. dmi_find_device() as some buggy BIOS's have the devices we are interested
  815. in marked as disabled */
  816. static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
  817. {
  818. int i, count;
  819. if (dm->type != 10)
  820. return;
  821. count = (dm->length - sizeof(struct dmi_header)) / 2;
  822. for (i = 0; i < count; i++) {
  823. const u8 *d = (char *)(dm + 1) + (i * 2);
  824. const char *name = ((char *) dm) + dm->length;
  825. u8 type = d[0];
  826. u8 s = d[1];
  827. if (!s)
  828. continue;
  829. s--;
  830. while (s > 0 && name[0]) {
  831. name += strlen(name) + 1;
  832. s--;
  833. }
  834. if (name[0] == 0) /* Bogus string reference */
  835. continue;
  836. dmi_check_onboard_device(type, name, adap);
  837. }
  838. }
  839. /* Register optional slaves */
  840. static void i801_probe_optional_slaves(struct i801_priv *priv)
  841. {
  842. /* Only register slaves on main SMBus channel */
  843. if (priv->features & FEATURE_IDF)
  844. return;
  845. if (apanel_addr) {
  846. struct i2c_board_info info;
  847. memset(&info, 0, sizeof(struct i2c_board_info));
  848. info.addr = apanel_addr;
  849. strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
  850. i2c_new_device(&priv->adapter, &info);
  851. }
  852. if (dmi_name_in_vendors("FUJITSU"))
  853. dmi_walk(dmi_check_onboard_devices, &priv->adapter);
  854. }
  855. #else
  856. static void __init input_apanel_init(void) {}
  857. static void i801_probe_optional_slaves(struct i801_priv *priv) {}
  858. #endif /* CONFIG_X86 && CONFIG_DMI */
  859. #if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
  860. defined CONFIG_DMI
  861. static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
  862. .gpio_chip = "gpio_ich",
  863. .values = { 0x02, 0x03 },
  864. .n_values = 2,
  865. .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
  866. .gpios = { 52, 53 },
  867. .n_gpios = 2,
  868. };
  869. static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
  870. .gpio_chip = "gpio_ich",
  871. .values = { 0x02, 0x03, 0x01 },
  872. .n_values = 3,
  873. .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
  874. .gpios = { 52, 53 },
  875. .n_gpios = 2,
  876. };
  877. static const struct dmi_system_id mux_dmi_table[] = {
  878. {
  879. .matches = {
  880. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  881. DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
  882. },
  883. .driver_data = &i801_mux_config_asus_z8_d12,
  884. },
  885. {
  886. .matches = {
  887. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  888. DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
  889. },
  890. .driver_data = &i801_mux_config_asus_z8_d12,
  891. },
  892. {
  893. .matches = {
  894. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  895. DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
  896. },
  897. .driver_data = &i801_mux_config_asus_z8_d12,
  898. },
  899. {
  900. .matches = {
  901. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  902. DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
  903. },
  904. .driver_data = &i801_mux_config_asus_z8_d12,
  905. },
  906. {
  907. .matches = {
  908. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  909. DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
  910. },
  911. .driver_data = &i801_mux_config_asus_z8_d12,
  912. },
  913. {
  914. .matches = {
  915. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  916. DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
  917. },
  918. .driver_data = &i801_mux_config_asus_z8_d12,
  919. },
  920. {
  921. .matches = {
  922. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  923. DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
  924. },
  925. .driver_data = &i801_mux_config_asus_z8_d18,
  926. },
  927. {
  928. .matches = {
  929. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  930. DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
  931. },
  932. .driver_data = &i801_mux_config_asus_z8_d18,
  933. },
  934. {
  935. .matches = {
  936. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  937. DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
  938. },
  939. .driver_data = &i801_mux_config_asus_z8_d12,
  940. },
  941. { }
  942. };
  943. /* Setup multiplexing if needed */
  944. static int i801_add_mux(struct i801_priv *priv)
  945. {
  946. struct device *dev = &priv->adapter.dev;
  947. const struct i801_mux_config *mux_config;
  948. struct i2c_mux_gpio_platform_data gpio_data;
  949. int err;
  950. if (!priv->mux_drvdata)
  951. return 0;
  952. mux_config = priv->mux_drvdata;
  953. /* Prepare the platform data */
  954. memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
  955. gpio_data.parent = priv->adapter.nr;
  956. gpio_data.values = mux_config->values;
  957. gpio_data.n_values = mux_config->n_values;
  958. gpio_data.classes = mux_config->classes;
  959. gpio_data.gpio_chip = mux_config->gpio_chip;
  960. gpio_data.gpios = mux_config->gpios;
  961. gpio_data.n_gpios = mux_config->n_gpios;
  962. gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
  963. /* Register the mux device */
  964. priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
  965. PLATFORM_DEVID_AUTO, &gpio_data,
  966. sizeof(struct i2c_mux_gpio_platform_data));
  967. if (IS_ERR(priv->mux_pdev)) {
  968. err = PTR_ERR(priv->mux_pdev);
  969. priv->mux_pdev = NULL;
  970. dev_err(dev, "Failed to register i2c-mux-gpio device\n");
  971. return err;
  972. }
  973. return 0;
  974. }
  975. static void i801_del_mux(struct i801_priv *priv)
  976. {
  977. if (priv->mux_pdev)
  978. platform_device_unregister(priv->mux_pdev);
  979. }
  980. static unsigned int i801_get_adapter_class(struct i801_priv *priv)
  981. {
  982. const struct dmi_system_id *id;
  983. const struct i801_mux_config *mux_config;
  984. unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  985. int i;
  986. id = dmi_first_match(mux_dmi_table);
  987. if (id) {
  988. /* Remove branch classes from trunk */
  989. mux_config = id->driver_data;
  990. for (i = 0; i < mux_config->n_values; i++)
  991. class &= ~mux_config->classes[i];
  992. /* Remember for later */
  993. priv->mux_drvdata = mux_config;
  994. }
  995. return class;
  996. }
  997. #else
  998. static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
  999. static inline void i801_del_mux(struct i801_priv *priv) { }
  1000. static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
  1001. {
  1002. return I2C_CLASS_HWMON | I2C_CLASS_SPD;
  1003. }
  1004. #endif
  1005. static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1006. {
  1007. unsigned char temp;
  1008. int err, i;
  1009. struct i801_priv *priv;
  1010. priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
  1011. if (!priv)
  1012. return -ENOMEM;
  1013. i2c_set_adapdata(&priv->adapter, priv);
  1014. priv->adapter.owner = THIS_MODULE;
  1015. priv->adapter.class = i801_get_adapter_class(priv);
  1016. priv->adapter.algo = &smbus_algorithm;
  1017. priv->pci_dev = dev;
  1018. switch (dev->device) {
  1019. case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
  1020. case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
  1021. case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
  1022. case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
  1023. case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
  1024. case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
  1025. priv->features |= FEATURE_IDF;
  1026. /* fall through */
  1027. default:
  1028. priv->features |= FEATURE_I2C_BLOCK_READ;
  1029. priv->features |= FEATURE_IRQ;
  1030. /* fall through */
  1031. case PCI_DEVICE_ID_INTEL_82801DB_3:
  1032. priv->features |= FEATURE_SMBUS_PEC;
  1033. priv->features |= FEATURE_BLOCK_BUFFER;
  1034. /* fall through */
  1035. case PCI_DEVICE_ID_INTEL_82801CA_3:
  1036. case PCI_DEVICE_ID_INTEL_82801BA_2:
  1037. case PCI_DEVICE_ID_INTEL_82801AB_3:
  1038. case PCI_DEVICE_ID_INTEL_82801AA_3:
  1039. break;
  1040. }
  1041. /* Disable features on user request */
  1042. for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
  1043. if (priv->features & disable_features & (1 << i))
  1044. dev_notice(&dev->dev, "%s disabled by user\n",
  1045. i801_feature_names[i]);
  1046. }
  1047. priv->features &= ~disable_features;
  1048. err = pcim_enable_device(dev);
  1049. if (err) {
  1050. dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
  1051. err);
  1052. return err;
  1053. }
  1054. pcim_pin_device(dev);
  1055. /* Determine the address of the SMBus area */
  1056. priv->smba = pci_resource_start(dev, SMBBAR);
  1057. if (!priv->smba) {
  1058. dev_err(&dev->dev,
  1059. "SMBus base address uninitialized, upgrade BIOS\n");
  1060. return -ENODEV;
  1061. }
  1062. err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
  1063. if (err) {
  1064. return -ENODEV;
  1065. }
  1066. err = pcim_iomap_regions(dev, 1 << SMBBAR,
  1067. dev_driver_string(&dev->dev));
  1068. if (err) {
  1069. dev_err(&dev->dev,
  1070. "Failed to request SMBus region 0x%lx-0x%Lx\n",
  1071. priv->smba,
  1072. (unsigned long long)pci_resource_end(dev, SMBBAR));
  1073. return err;
  1074. }
  1075. pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
  1076. priv->original_hstcfg = temp;
  1077. temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
  1078. if (!(temp & SMBHSTCFG_HST_EN)) {
  1079. dev_info(&dev->dev, "Enabling SMBus device\n");
  1080. temp |= SMBHSTCFG_HST_EN;
  1081. }
  1082. pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
  1083. if (temp & SMBHSTCFG_SMB_SMI_EN) {
  1084. dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
  1085. /* Disable SMBus interrupt feature if SMBus using SMI# */
  1086. priv->features &= ~FEATURE_IRQ;
  1087. }
  1088. /* Clear special mode bits */
  1089. if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
  1090. outb_p(inb_p(SMBAUXCTL(priv)) &
  1091. ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
  1092. /* Default timeout in interrupt mode: 200 ms */
  1093. priv->adapter.timeout = HZ / 5;
  1094. if (priv->features & FEATURE_IRQ) {
  1095. u16 pcictl, pcists;
  1096. /* Complain if an interrupt is already pending */
  1097. pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
  1098. if (pcists & SMBPCISTS_INTS)
  1099. dev_warn(&dev->dev, "An interrupt is pending!\n");
  1100. /* Check if interrupts have been disabled */
  1101. pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
  1102. if (pcictl & SMBPCICTL_INTDIS) {
  1103. dev_info(&dev->dev, "Interrupts are disabled\n");
  1104. priv->features &= ~FEATURE_IRQ;
  1105. }
  1106. }
  1107. if (priv->features & FEATURE_IRQ) {
  1108. init_waitqueue_head(&priv->waitq);
  1109. err = devm_request_irq(&dev->dev, dev->irq, i801_isr,
  1110. IRQF_SHARED,
  1111. dev_driver_string(&dev->dev), priv);
  1112. if (err) {
  1113. dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
  1114. dev->irq, err);
  1115. priv->features &= ~FEATURE_IRQ;
  1116. }
  1117. }
  1118. dev_info(&dev->dev, "SMBus using %s\n",
  1119. priv->features & FEATURE_IRQ ? "PCI interrupt" : "polling");
  1120. /* set up the sysfs linkage to our parent device */
  1121. priv->adapter.dev.parent = &dev->dev;
  1122. /* Retry up to 3 times on lost arbitration */
  1123. priv->adapter.retries = 3;
  1124. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  1125. "SMBus I801 adapter at %04lx", priv->smba);
  1126. err = i2c_add_adapter(&priv->adapter);
  1127. if (err) {
  1128. dev_err(&dev->dev, "Failed to add SMBus adapter\n");
  1129. return err;
  1130. }
  1131. i801_probe_optional_slaves(priv);
  1132. /* We ignore errors - multiplexing is optional */
  1133. i801_add_mux(priv);
  1134. pci_set_drvdata(dev, priv);
  1135. return 0;
  1136. }
  1137. static void i801_remove(struct pci_dev *dev)
  1138. {
  1139. struct i801_priv *priv = pci_get_drvdata(dev);
  1140. i801_del_mux(priv);
  1141. i2c_del_adapter(&priv->adapter);
  1142. pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
  1143. /*
  1144. * do not call pci_disable_device(dev) since it can cause hard hangs on
  1145. * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
  1146. */
  1147. }
  1148. #ifdef CONFIG_PM
  1149. static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
  1150. {
  1151. struct i801_priv *priv = pci_get_drvdata(dev);
  1152. pci_save_state(dev);
  1153. pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
  1154. pci_set_power_state(dev, pci_choose_state(dev, mesg));
  1155. return 0;
  1156. }
  1157. static int i801_resume(struct pci_dev *dev)
  1158. {
  1159. pci_set_power_state(dev, PCI_D0);
  1160. pci_restore_state(dev);
  1161. return 0;
  1162. }
  1163. #else
  1164. #define i801_suspend NULL
  1165. #define i801_resume NULL
  1166. #endif
  1167. static struct pci_driver i801_driver = {
  1168. .name = "i801_smbus",
  1169. .id_table = i801_ids,
  1170. .probe = i801_probe,
  1171. .remove = i801_remove,
  1172. .suspend = i801_suspend,
  1173. .resume = i801_resume,
  1174. };
  1175. static int __init i2c_i801_init(void)
  1176. {
  1177. if (dmi_name_in_vendors("FUJITSU"))
  1178. input_apanel_init();
  1179. return pci_register_driver(&i801_driver);
  1180. }
  1181. static void __exit i2c_i801_exit(void)
  1182. {
  1183. pci_unregister_driver(&i801_driver);
  1184. }
  1185. MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, Jean Delvare <jdelvare@suse.de>");
  1186. MODULE_DESCRIPTION("I801 SMBus driver");
  1187. MODULE_LICENSE("GPL");
  1188. module_init(i2c_i801_init);
  1189. module_exit(i2c_i801_exit);