i2c-i801.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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. * Braswell (SOC) 0x2292 32 hard yes yes yes
  59. * Sunrise Point-H (PCH) 0xa123 32 hard yes yes yes
  60. * Sunrise Point-LP (PCH) 0x9d23 32 hard yes yes yes
  61. * DNV (SOC) 0x19df 32 hard yes yes yes
  62. * Broxton (SOC) 0x5ad4 32 hard yes yes yes
  63. * Lewisburg (PCH) 0xa1a3 32 hard yes yes yes
  64. * Lewisburg Supersku (PCH) 0xa223 32 hard yes yes yes
  65. * Kaby Lake PCH-H (PCH) 0xa2a3 32 hard yes yes yes
  66. * Gemini Lake (SOC) 0x31d4 32 hard yes yes yes
  67. * Cannon Lake-H (PCH) 0xa323 32 hard yes yes yes
  68. * Cannon Lake-LP (PCH) 0x9da3 32 hard yes yes yes
  69. * Cedar Fork (PCH) 0x18df 32 hard yes yes yes
  70. * Ice Lake-LP (PCH) 0x34a3 32 hard yes yes yes
  71. * Comet Lake (PCH) 0x02a3 32 hard yes yes yes
  72. *
  73. * Features supported by this driver:
  74. * Software PEC no
  75. * Hardware PEC yes
  76. * Block buffer yes
  77. * Block process call transaction no
  78. * I2C block read transaction yes (doesn't use the block buffer)
  79. * Slave mode no
  80. * SMBus Host Notify yes
  81. * Interrupt processing yes
  82. *
  83. * See the file Documentation/i2c/busses/i2c-i801 for details.
  84. */
  85. #include <linux/interrupt.h>
  86. #include <linux/module.h>
  87. #include <linux/pci.h>
  88. #include <linux/kernel.h>
  89. #include <linux/stddef.h>
  90. #include <linux/delay.h>
  91. #include <linux/ioport.h>
  92. #include <linux/init.h>
  93. #include <linux/i2c.h>
  94. #include <linux/i2c-smbus.h>
  95. #include <linux/acpi.h>
  96. #include <linux/io.h>
  97. #include <linux/dmi.h>
  98. #include <linux/slab.h>
  99. #include <linux/wait.h>
  100. #include <linux/err.h>
  101. #include <linux/platform_device.h>
  102. #include <linux/platform_data/itco_wdt.h>
  103. #include <linux/pm_runtime.h>
  104. #if IS_ENABLED(CONFIG_I2C_MUX_GPIO) && defined CONFIG_DMI
  105. #include <linux/gpio.h>
  106. #include <linux/platform_data/i2c-mux-gpio.h>
  107. #endif
  108. /* I801 SMBus address offsets */
  109. #define SMBHSTSTS(p) (0 + (p)->smba)
  110. #define SMBHSTCNT(p) (2 + (p)->smba)
  111. #define SMBHSTCMD(p) (3 + (p)->smba)
  112. #define SMBHSTADD(p) (4 + (p)->smba)
  113. #define SMBHSTDAT0(p) (5 + (p)->smba)
  114. #define SMBHSTDAT1(p) (6 + (p)->smba)
  115. #define SMBBLKDAT(p) (7 + (p)->smba)
  116. #define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
  117. #define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
  118. #define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
  119. #define SMBSLVSTS(p) (16 + (p)->smba) /* ICH3 and later */
  120. #define SMBSLVCMD(p) (17 + (p)->smba) /* ICH3 and later */
  121. #define SMBNTFDADD(p) (20 + (p)->smba) /* ICH3 and later */
  122. /* PCI Address Constants */
  123. #define SMBBAR 4
  124. #define SMBPCICTL 0x004
  125. #define SMBPCISTS 0x006
  126. #define SMBHSTCFG 0x040
  127. #define TCOBASE 0x050
  128. #define TCOCTL 0x054
  129. #define ACPIBASE 0x040
  130. #define ACPIBASE_SMI_OFF 0x030
  131. #define ACPICTRL 0x044
  132. #define ACPICTRL_EN 0x080
  133. #define SBREG_BAR 0x10
  134. #define SBREG_SMBCTRL 0xc6000c
  135. #define SBREG_SMBCTRL_DNV 0xcf000c
  136. /* Host status bits for SMBPCISTS */
  137. #define SMBPCISTS_INTS BIT(3)
  138. /* Control bits for SMBPCICTL */
  139. #define SMBPCICTL_INTDIS BIT(10)
  140. /* Host configuration bits for SMBHSTCFG */
  141. #define SMBHSTCFG_HST_EN BIT(0)
  142. #define SMBHSTCFG_SMB_SMI_EN BIT(1)
  143. #define SMBHSTCFG_I2C_EN BIT(2)
  144. #define SMBHSTCFG_SPD_WD BIT(4)
  145. /* TCO configuration bits for TCOCTL */
  146. #define TCOCTL_EN BIT(8)
  147. /* Auxiliary status register bits, ICH4+ only */
  148. #define SMBAUXSTS_CRCE BIT(0)
  149. #define SMBAUXSTS_STCO BIT(1)
  150. /* Auxiliary control register bits, ICH4+ only */
  151. #define SMBAUXCTL_CRC BIT(0)
  152. #define SMBAUXCTL_E32B BIT(1)
  153. /* Other settings */
  154. #define MAX_RETRIES 400
  155. /* I801 command constants */
  156. #define I801_QUICK 0x00
  157. #define I801_BYTE 0x04
  158. #define I801_BYTE_DATA 0x08
  159. #define I801_WORD_DATA 0x0C
  160. #define I801_PROC_CALL 0x10 /* unimplemented */
  161. #define I801_BLOCK_DATA 0x14
  162. #define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
  163. /* I801 Host Control register bits */
  164. #define SMBHSTCNT_INTREN BIT(0)
  165. #define SMBHSTCNT_KILL BIT(1)
  166. #define SMBHSTCNT_LAST_BYTE BIT(5)
  167. #define SMBHSTCNT_START BIT(6)
  168. #define SMBHSTCNT_PEC_EN BIT(7) /* ICH3 and later */
  169. /* I801 Hosts Status register bits */
  170. #define SMBHSTSTS_BYTE_DONE BIT(7)
  171. #define SMBHSTSTS_INUSE_STS BIT(6)
  172. #define SMBHSTSTS_SMBALERT_STS BIT(5)
  173. #define SMBHSTSTS_FAILED BIT(4)
  174. #define SMBHSTSTS_BUS_ERR BIT(3)
  175. #define SMBHSTSTS_DEV_ERR BIT(2)
  176. #define SMBHSTSTS_INTR BIT(1)
  177. #define SMBHSTSTS_HOST_BUSY BIT(0)
  178. /* Host Notify Status register bits */
  179. #define SMBSLVSTS_HST_NTFY_STS BIT(0)
  180. /* Host Notify Command register bits */
  181. #define SMBSLVCMD_HST_NTFY_INTREN BIT(0)
  182. #define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
  183. SMBHSTSTS_DEV_ERR)
  184. #define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
  185. STATUS_ERROR_FLAGS)
  186. /* Older devices have their ID defined in <linux/pci_ids.h> */
  187. #define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
  188. #define PCI_DEVICE_ID_INTEL_CDF_SMBUS 0x18df
  189. #define PCI_DEVICE_ID_INTEL_DNV_SMBUS 0x19df
  190. #define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
  191. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
  192. /* Patsburg also has three 'Integrated Device Function' SMBus controllers */
  193. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
  194. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
  195. #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
  196. #define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
  197. #define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
  198. #define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
  199. #define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
  200. #define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
  201. #define PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS 0x31d4
  202. #define PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS 0x34a3
  203. #define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
  204. #define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
  205. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
  206. #define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
  207. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
  208. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
  209. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
  210. #define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
  211. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
  212. #define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
  213. #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23
  214. #define PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS 0x9da3
  215. #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
  216. #define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3
  217. #define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223
  218. #define PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS 0xa2a3
  219. #define PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS 0xa323
  220. #define PCI_DEVICE_ID_INTEL_COMETLAKE_SMBUS 0x02a3
  221. struct i801_mux_config {
  222. char *gpio_chip;
  223. unsigned values[3];
  224. int n_values;
  225. unsigned classes[3];
  226. unsigned gpios[2]; /* Relative to gpio_chip->base */
  227. int n_gpios;
  228. };
  229. struct i801_priv {
  230. struct i2c_adapter adapter;
  231. unsigned long smba;
  232. unsigned char original_hstcfg;
  233. unsigned char original_slvcmd;
  234. struct pci_dev *pci_dev;
  235. unsigned int features;
  236. /* isr processing */
  237. wait_queue_head_t waitq;
  238. u8 status;
  239. /* Command state used by isr for byte-by-byte block transactions */
  240. u8 cmd;
  241. bool is_read;
  242. int count;
  243. int len;
  244. u8 *data;
  245. #if IS_ENABLED(CONFIG_I2C_MUX_GPIO) && defined CONFIG_DMI
  246. const struct i801_mux_config *mux_drvdata;
  247. struct platform_device *mux_pdev;
  248. #endif
  249. struct platform_device *tco_pdev;
  250. /*
  251. * If set to true the host controller registers are reserved for
  252. * ACPI AML use. Protected by acpi_lock.
  253. */
  254. bool acpi_reserved;
  255. struct mutex acpi_lock;
  256. };
  257. #define FEATURE_SMBUS_PEC BIT(0)
  258. #define FEATURE_BLOCK_BUFFER BIT(1)
  259. #define FEATURE_BLOCK_PROC BIT(2)
  260. #define FEATURE_I2C_BLOCK_READ BIT(3)
  261. #define FEATURE_IRQ BIT(4)
  262. #define FEATURE_HOST_NOTIFY BIT(5)
  263. /* Not really a feature, but it's convenient to handle it as such */
  264. #define FEATURE_IDF BIT(15)
  265. #define FEATURE_TCO BIT(16)
  266. static const char *i801_feature_names[] = {
  267. "SMBus PEC",
  268. "Block buffer",
  269. "Block process call",
  270. "I2C block read",
  271. "Interrupt",
  272. "SMBus Host Notify",
  273. };
  274. static unsigned int disable_features;
  275. module_param(disable_features, uint, S_IRUGO | S_IWUSR);
  276. MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
  277. "\t\t 0x01 disable SMBus PEC\n"
  278. "\t\t 0x02 disable the block buffer\n"
  279. "\t\t 0x08 disable the I2C block read functionality\n"
  280. "\t\t 0x10 don't use interrupts\n"
  281. "\t\t 0x20 disable SMBus Host Notify ");
  282. /* Make sure the SMBus host is ready to start transmitting.
  283. Return 0 if it is, -EBUSY if it is not. */
  284. static int i801_check_pre(struct i801_priv *priv)
  285. {
  286. int status;
  287. status = inb_p(SMBHSTSTS(priv));
  288. if (status & SMBHSTSTS_HOST_BUSY) {
  289. dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
  290. return -EBUSY;
  291. }
  292. status &= STATUS_FLAGS;
  293. if (status) {
  294. dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
  295. status);
  296. outb_p(status, SMBHSTSTS(priv));
  297. status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
  298. if (status) {
  299. dev_err(&priv->pci_dev->dev,
  300. "Failed clearing status flags (%02x)\n",
  301. status);
  302. return -EBUSY;
  303. }
  304. }
  305. /*
  306. * Clear CRC status if needed.
  307. * During normal operation, i801_check_post() takes care
  308. * of it after every operation. We do it here only in case
  309. * the hardware was already in this state when the driver
  310. * started.
  311. */
  312. if (priv->features & FEATURE_SMBUS_PEC) {
  313. status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
  314. if (status) {
  315. dev_dbg(&priv->pci_dev->dev,
  316. "Clearing aux status flags (%02x)\n", status);
  317. outb_p(status, SMBAUXSTS(priv));
  318. status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
  319. if (status) {
  320. dev_err(&priv->pci_dev->dev,
  321. "Failed clearing aux status flags (%02x)\n",
  322. status);
  323. return -EBUSY;
  324. }
  325. }
  326. }
  327. return 0;
  328. }
  329. /*
  330. * Convert the status register to an error code, and clear it.
  331. * Note that status only contains the bits we want to clear, not the
  332. * actual register value.
  333. */
  334. static int i801_check_post(struct i801_priv *priv, int status)
  335. {
  336. int result = 0;
  337. /*
  338. * If the SMBus is still busy, we give up
  339. * Note: This timeout condition only happens when using polling
  340. * transactions. For interrupt operation, NAK/timeout is indicated by
  341. * DEV_ERR.
  342. */
  343. if (unlikely(status < 0)) {
  344. dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
  345. /* try to stop the current command */
  346. dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
  347. outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
  348. SMBHSTCNT(priv));
  349. usleep_range(1000, 2000);
  350. outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
  351. SMBHSTCNT(priv));
  352. /* Check if it worked */
  353. status = inb_p(SMBHSTSTS(priv));
  354. if ((status & SMBHSTSTS_HOST_BUSY) ||
  355. !(status & SMBHSTSTS_FAILED))
  356. dev_err(&priv->pci_dev->dev,
  357. "Failed terminating the transaction\n");
  358. outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
  359. return -ETIMEDOUT;
  360. }
  361. if (status & SMBHSTSTS_FAILED) {
  362. result = -EIO;
  363. dev_err(&priv->pci_dev->dev, "Transaction failed\n");
  364. }
  365. if (status & SMBHSTSTS_DEV_ERR) {
  366. /*
  367. * This may be a PEC error, check and clear it.
  368. *
  369. * AUXSTS is handled differently from HSTSTS.
  370. * For HSTSTS, i801_isr() or i801_wait_intr()
  371. * has already cleared the error bits in hardware,
  372. * and we are passed a copy of the original value
  373. * in "status".
  374. * For AUXSTS, the hardware register is left
  375. * for us to handle here.
  376. * This is asymmetric, slightly iffy, but safe,
  377. * since all this code is serialized and the CRCE
  378. * bit is harmless as long as it's cleared before
  379. * the next operation.
  380. */
  381. if ((priv->features & FEATURE_SMBUS_PEC) &&
  382. (inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE)) {
  383. outb_p(SMBAUXSTS_CRCE, SMBAUXSTS(priv));
  384. result = -EBADMSG;
  385. dev_dbg(&priv->pci_dev->dev, "PEC error\n");
  386. } else {
  387. result = -ENXIO;
  388. dev_dbg(&priv->pci_dev->dev, "No response\n");
  389. }
  390. }
  391. if (status & SMBHSTSTS_BUS_ERR) {
  392. result = -EAGAIN;
  393. dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
  394. }
  395. /* Clear status flags except BYTE_DONE, to be cleared by caller */
  396. outb_p(status, SMBHSTSTS(priv));
  397. return result;
  398. }
  399. /* Wait for BUSY being cleared and either INTR or an error flag being set */
  400. static int i801_wait_intr(struct i801_priv *priv)
  401. {
  402. int timeout = 0;
  403. int status;
  404. /* We will always wait for a fraction of a second! */
  405. do {
  406. usleep_range(250, 500);
  407. status = inb_p(SMBHSTSTS(priv));
  408. } while (((status & SMBHSTSTS_HOST_BUSY) ||
  409. !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
  410. (timeout++ < MAX_RETRIES));
  411. if (timeout > MAX_RETRIES) {
  412. dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
  413. return -ETIMEDOUT;
  414. }
  415. return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
  416. }
  417. /* Wait for either BYTE_DONE or an error flag being set */
  418. static int i801_wait_byte_done(struct i801_priv *priv)
  419. {
  420. int timeout = 0;
  421. int status;
  422. /* We will always wait for a fraction of a second! */
  423. do {
  424. usleep_range(250, 500);
  425. status = inb_p(SMBHSTSTS(priv));
  426. } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
  427. (timeout++ < MAX_RETRIES));
  428. if (timeout > MAX_RETRIES) {
  429. dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
  430. return -ETIMEDOUT;
  431. }
  432. return status & STATUS_ERROR_FLAGS;
  433. }
  434. static int i801_transaction(struct i801_priv *priv, int xact)
  435. {
  436. int status;
  437. int result;
  438. const struct i2c_adapter *adap = &priv->adapter;
  439. result = i801_check_pre(priv);
  440. if (result < 0)
  441. return result;
  442. if (priv->features & FEATURE_IRQ) {
  443. outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
  444. SMBHSTCNT(priv));
  445. result = wait_event_timeout(priv->waitq,
  446. (status = priv->status),
  447. adap->timeout);
  448. if (!result) {
  449. status = -ETIMEDOUT;
  450. dev_warn(&priv->pci_dev->dev,
  451. "Timeout waiting for interrupt!\n");
  452. }
  453. priv->status = 0;
  454. return i801_check_post(priv, status);
  455. }
  456. /* the current contents of SMBHSTCNT can be overwritten, since PEC,
  457. * SMBSCMD are passed in xact */
  458. outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
  459. status = i801_wait_intr(priv);
  460. return i801_check_post(priv, status);
  461. }
  462. static int i801_block_transaction_by_block(struct i801_priv *priv,
  463. union i2c_smbus_data *data,
  464. char read_write, int hwpec)
  465. {
  466. int i, len;
  467. int status;
  468. inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
  469. /* Use 32-byte buffer to process this transaction */
  470. if (read_write == I2C_SMBUS_WRITE) {
  471. len = data->block[0];
  472. outb_p(len, SMBHSTDAT0(priv));
  473. for (i = 0; i < len; i++)
  474. outb_p(data->block[i+1], SMBBLKDAT(priv));
  475. }
  476. status = i801_transaction(priv, I801_BLOCK_DATA |
  477. (hwpec ? SMBHSTCNT_PEC_EN : 0));
  478. if (status)
  479. return status;
  480. if (read_write == I2C_SMBUS_READ) {
  481. len = inb_p(SMBHSTDAT0(priv));
  482. if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
  483. return -EPROTO;
  484. data->block[0] = len;
  485. for (i = 0; i < len; i++)
  486. data->block[i + 1] = inb_p(SMBBLKDAT(priv));
  487. }
  488. return 0;
  489. }
  490. static void i801_isr_byte_done(struct i801_priv *priv)
  491. {
  492. if (priv->is_read) {
  493. /* For SMBus block reads, length is received with first byte */
  494. if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
  495. (priv->count == 0)) {
  496. priv->len = inb_p(SMBHSTDAT0(priv));
  497. if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
  498. dev_err(&priv->pci_dev->dev,
  499. "Illegal SMBus block read size %d\n",
  500. priv->len);
  501. /* FIXME: Recover */
  502. priv->len = I2C_SMBUS_BLOCK_MAX;
  503. } else {
  504. dev_dbg(&priv->pci_dev->dev,
  505. "SMBus block read size is %d\n",
  506. priv->len);
  507. }
  508. priv->data[-1] = priv->len;
  509. }
  510. /* Read next byte */
  511. if (priv->count < priv->len)
  512. priv->data[priv->count++] = inb(SMBBLKDAT(priv));
  513. else
  514. dev_dbg(&priv->pci_dev->dev,
  515. "Discarding extra byte on block read\n");
  516. /* Set LAST_BYTE for last byte of read transaction */
  517. if (priv->count == priv->len - 1)
  518. outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
  519. SMBHSTCNT(priv));
  520. } else if (priv->count < priv->len - 1) {
  521. /* Write next byte, except for IRQ after last byte */
  522. outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
  523. }
  524. /* Clear BYTE_DONE to continue with next byte */
  525. outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
  526. }
  527. static irqreturn_t i801_host_notify_isr(struct i801_priv *priv)
  528. {
  529. unsigned short addr;
  530. addr = inb_p(SMBNTFDADD(priv)) >> 1;
  531. /*
  532. * With the tested platforms, reading SMBNTFDDAT (22 + (p)->smba)
  533. * always returns 0. Our current implementation doesn't provide
  534. * data, so we just ignore it.
  535. */
  536. i2c_handle_smbus_host_notify(&priv->adapter, addr);
  537. /* clear Host Notify bit and return */
  538. outb_p(SMBSLVSTS_HST_NTFY_STS, SMBSLVSTS(priv));
  539. return IRQ_HANDLED;
  540. }
  541. /*
  542. * There are three kinds of interrupts:
  543. *
  544. * 1) i801 signals transaction completion with one of these interrupts:
  545. * INTR - Success
  546. * DEV_ERR - Invalid command, NAK or communication timeout
  547. * BUS_ERR - SMI# transaction collision
  548. * FAILED - transaction was canceled due to a KILL request
  549. * When any of these occur, update ->status and wake up the waitq.
  550. * ->status must be cleared before kicking off the next transaction.
  551. *
  552. * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
  553. * occurs for each byte of a byte-by-byte to prepare the next byte.
  554. *
  555. * 3) Host Notify interrupts
  556. */
  557. static irqreturn_t i801_isr(int irq, void *dev_id)
  558. {
  559. struct i801_priv *priv = dev_id;
  560. u16 pcists;
  561. u8 status;
  562. /* Confirm this is our interrupt */
  563. pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
  564. if (!(pcists & SMBPCISTS_INTS))
  565. return IRQ_NONE;
  566. if (priv->features & FEATURE_HOST_NOTIFY) {
  567. status = inb_p(SMBSLVSTS(priv));
  568. if (status & SMBSLVSTS_HST_NTFY_STS)
  569. return i801_host_notify_isr(priv);
  570. }
  571. status = inb_p(SMBHSTSTS(priv));
  572. if (status & SMBHSTSTS_BYTE_DONE)
  573. i801_isr_byte_done(priv);
  574. /*
  575. * Clear irq sources and report transaction result.
  576. * ->status must be cleared before the next transaction is started.
  577. */
  578. status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
  579. if (status) {
  580. outb_p(status, SMBHSTSTS(priv));
  581. priv->status = status;
  582. wake_up(&priv->waitq);
  583. }
  584. return IRQ_HANDLED;
  585. }
  586. /*
  587. * For "byte-by-byte" block transactions:
  588. * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
  589. * I2C read uses cmd=I801_I2C_BLOCK_DATA
  590. */
  591. static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
  592. union i2c_smbus_data *data,
  593. char read_write, int command,
  594. int hwpec)
  595. {
  596. int i, len;
  597. int smbcmd;
  598. int status;
  599. int result;
  600. const struct i2c_adapter *adap = &priv->adapter;
  601. result = i801_check_pre(priv);
  602. if (result < 0)
  603. return result;
  604. len = data->block[0];
  605. if (read_write == I2C_SMBUS_WRITE) {
  606. outb_p(len, SMBHSTDAT0(priv));
  607. outb_p(data->block[1], SMBBLKDAT(priv));
  608. }
  609. if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
  610. read_write == I2C_SMBUS_READ)
  611. smbcmd = I801_I2C_BLOCK_DATA;
  612. else
  613. smbcmd = I801_BLOCK_DATA;
  614. if (priv->features & FEATURE_IRQ) {
  615. priv->is_read = (read_write == I2C_SMBUS_READ);
  616. if (len == 1 && priv->is_read)
  617. smbcmd |= SMBHSTCNT_LAST_BYTE;
  618. priv->cmd = smbcmd | SMBHSTCNT_INTREN;
  619. priv->len = len;
  620. priv->count = 0;
  621. priv->data = &data->block[1];
  622. outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
  623. result = wait_event_timeout(priv->waitq,
  624. (status = priv->status),
  625. adap->timeout);
  626. if (!result) {
  627. status = -ETIMEDOUT;
  628. dev_warn(&priv->pci_dev->dev,
  629. "Timeout waiting for interrupt!\n");
  630. }
  631. priv->status = 0;
  632. return i801_check_post(priv, status);
  633. }
  634. for (i = 1; i <= len; i++) {
  635. if (i == len && read_write == I2C_SMBUS_READ)
  636. smbcmd |= SMBHSTCNT_LAST_BYTE;
  637. outb_p(smbcmd, SMBHSTCNT(priv));
  638. if (i == 1)
  639. outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
  640. SMBHSTCNT(priv));
  641. status = i801_wait_byte_done(priv);
  642. if (status)
  643. goto exit;
  644. if (i == 1 && read_write == I2C_SMBUS_READ
  645. && command != I2C_SMBUS_I2C_BLOCK_DATA) {
  646. len = inb_p(SMBHSTDAT0(priv));
  647. if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
  648. dev_err(&priv->pci_dev->dev,
  649. "Illegal SMBus block read size %d\n",
  650. len);
  651. /* Recover */
  652. while (inb_p(SMBHSTSTS(priv)) &
  653. SMBHSTSTS_HOST_BUSY)
  654. outb_p(SMBHSTSTS_BYTE_DONE,
  655. SMBHSTSTS(priv));
  656. outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
  657. return -EPROTO;
  658. }
  659. data->block[0] = len;
  660. }
  661. /* Retrieve/store value in SMBBLKDAT */
  662. if (read_write == I2C_SMBUS_READ)
  663. data->block[i] = inb_p(SMBBLKDAT(priv));
  664. if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
  665. outb_p(data->block[i+1], SMBBLKDAT(priv));
  666. /* signals SMBBLKDAT ready */
  667. outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
  668. }
  669. status = i801_wait_intr(priv);
  670. exit:
  671. return i801_check_post(priv, status);
  672. }
  673. static int i801_set_block_buffer_mode(struct i801_priv *priv)
  674. {
  675. outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
  676. if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
  677. return -EIO;
  678. return 0;
  679. }
  680. /* Block transaction function */
  681. static int i801_block_transaction(struct i801_priv *priv,
  682. union i2c_smbus_data *data, char read_write,
  683. int command, int hwpec)
  684. {
  685. int result = 0;
  686. unsigned char hostc;
  687. if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
  688. if (read_write == I2C_SMBUS_WRITE) {
  689. /* set I2C_EN bit in configuration register */
  690. pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
  691. pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
  692. hostc | SMBHSTCFG_I2C_EN);
  693. } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
  694. dev_err(&priv->pci_dev->dev,
  695. "I2C block read is unsupported!\n");
  696. return -EOPNOTSUPP;
  697. }
  698. }
  699. if (read_write == I2C_SMBUS_WRITE
  700. || command == I2C_SMBUS_I2C_BLOCK_DATA) {
  701. if (data->block[0] < 1)
  702. data->block[0] = 1;
  703. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  704. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  705. } else {
  706. data->block[0] = 32; /* max for SMBus block reads */
  707. }
  708. /* Experience has shown that the block buffer can only be used for
  709. SMBus (not I2C) block transactions, even though the datasheet
  710. doesn't mention this limitation. */
  711. if ((priv->features & FEATURE_BLOCK_BUFFER)
  712. && command != I2C_SMBUS_I2C_BLOCK_DATA
  713. && i801_set_block_buffer_mode(priv) == 0)
  714. result = i801_block_transaction_by_block(priv, data,
  715. read_write, hwpec);
  716. else
  717. result = i801_block_transaction_byte_by_byte(priv, data,
  718. read_write,
  719. command, hwpec);
  720. if (command == I2C_SMBUS_I2C_BLOCK_DATA
  721. && read_write == I2C_SMBUS_WRITE) {
  722. /* restore saved configuration register value */
  723. pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
  724. }
  725. return result;
  726. }
  727. /* Return negative errno on error. */
  728. static s32 i801_access(struct i2c_adapter *adap, u16 addr,
  729. unsigned short flags, char read_write, u8 command,
  730. int size, union i2c_smbus_data *data)
  731. {
  732. int hwpec;
  733. int block = 0;
  734. int ret = 0, xact = 0;
  735. struct i801_priv *priv = i2c_get_adapdata(adap);
  736. mutex_lock(&priv->acpi_lock);
  737. if (priv->acpi_reserved) {
  738. mutex_unlock(&priv->acpi_lock);
  739. return -EBUSY;
  740. }
  741. pm_runtime_get_sync(&priv->pci_dev->dev);
  742. hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
  743. && size != I2C_SMBUS_QUICK
  744. && size != I2C_SMBUS_I2C_BLOCK_DATA;
  745. switch (size) {
  746. case I2C_SMBUS_QUICK:
  747. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  748. SMBHSTADD(priv));
  749. xact = I801_QUICK;
  750. break;
  751. case I2C_SMBUS_BYTE:
  752. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  753. SMBHSTADD(priv));
  754. if (read_write == I2C_SMBUS_WRITE)
  755. outb_p(command, SMBHSTCMD(priv));
  756. xact = I801_BYTE;
  757. break;
  758. case I2C_SMBUS_BYTE_DATA:
  759. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  760. SMBHSTADD(priv));
  761. outb_p(command, SMBHSTCMD(priv));
  762. if (read_write == I2C_SMBUS_WRITE)
  763. outb_p(data->byte, SMBHSTDAT0(priv));
  764. xact = I801_BYTE_DATA;
  765. break;
  766. case I2C_SMBUS_WORD_DATA:
  767. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  768. SMBHSTADD(priv));
  769. outb_p(command, SMBHSTCMD(priv));
  770. if (read_write == I2C_SMBUS_WRITE) {
  771. outb_p(data->word & 0xff, SMBHSTDAT0(priv));
  772. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
  773. }
  774. xact = I801_WORD_DATA;
  775. break;
  776. case I2C_SMBUS_BLOCK_DATA:
  777. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  778. SMBHSTADD(priv));
  779. outb_p(command, SMBHSTCMD(priv));
  780. block = 1;
  781. break;
  782. case I2C_SMBUS_I2C_BLOCK_DATA:
  783. /*
  784. * NB: page 240 of ICH5 datasheet shows that the R/#W
  785. * bit should be cleared here, even when reading.
  786. * However if SPD Write Disable is set (Lynx Point and later),
  787. * the read will fail if we don't set the R/#W bit.
  788. */
  789. outb_p(((addr & 0x7f) << 1) |
  790. ((priv->original_hstcfg & SMBHSTCFG_SPD_WD) ?
  791. (read_write & 0x01) : 0),
  792. SMBHSTADD(priv));
  793. if (read_write == I2C_SMBUS_READ) {
  794. /* NB: page 240 of ICH5 datasheet also shows
  795. * that DATA1 is the cmd field when reading */
  796. outb_p(command, SMBHSTDAT1(priv));
  797. } else
  798. outb_p(command, SMBHSTCMD(priv));
  799. block = 1;
  800. break;
  801. default:
  802. dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
  803. size);
  804. ret = -EOPNOTSUPP;
  805. goto out;
  806. }
  807. if (hwpec) /* enable/disable hardware PEC */
  808. outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
  809. else
  810. outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
  811. SMBAUXCTL(priv));
  812. if (block)
  813. ret = i801_block_transaction(priv, data, read_write, size,
  814. hwpec);
  815. else
  816. ret = i801_transaction(priv, xact);
  817. /* Some BIOSes don't like it when PEC is enabled at reboot or resume
  818. time, so we forcibly disable it after every transaction. Turn off
  819. E32B for the same reason. */
  820. if (hwpec || block)
  821. outb_p(inb_p(SMBAUXCTL(priv)) &
  822. ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
  823. if (block)
  824. goto out;
  825. if (ret)
  826. goto out;
  827. if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
  828. goto out;
  829. switch (xact & 0x7f) {
  830. case I801_BYTE: /* Result put in SMBHSTDAT0 */
  831. case I801_BYTE_DATA:
  832. data->byte = inb_p(SMBHSTDAT0(priv));
  833. break;
  834. case I801_WORD_DATA:
  835. data->word = inb_p(SMBHSTDAT0(priv)) +
  836. (inb_p(SMBHSTDAT1(priv)) << 8);
  837. break;
  838. }
  839. out:
  840. pm_runtime_mark_last_busy(&priv->pci_dev->dev);
  841. pm_runtime_put_autosuspend(&priv->pci_dev->dev);
  842. mutex_unlock(&priv->acpi_lock);
  843. return ret;
  844. }
  845. static u32 i801_func(struct i2c_adapter *adapter)
  846. {
  847. struct i801_priv *priv = i2c_get_adapdata(adapter);
  848. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  849. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  850. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
  851. ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
  852. ((priv->features & FEATURE_I2C_BLOCK_READ) ?
  853. I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0) |
  854. ((priv->features & FEATURE_HOST_NOTIFY) ?
  855. I2C_FUNC_SMBUS_HOST_NOTIFY : 0);
  856. }
  857. static void i801_enable_host_notify(struct i2c_adapter *adapter)
  858. {
  859. struct i801_priv *priv = i2c_get_adapdata(adapter);
  860. if (!(priv->features & FEATURE_HOST_NOTIFY))
  861. return;
  862. if (!(SMBSLVCMD_HST_NTFY_INTREN & priv->original_slvcmd))
  863. outb_p(SMBSLVCMD_HST_NTFY_INTREN | priv->original_slvcmd,
  864. SMBSLVCMD(priv));
  865. /* clear Host Notify bit to allow a new notification */
  866. outb_p(SMBSLVSTS_HST_NTFY_STS, SMBSLVSTS(priv));
  867. }
  868. static void i801_disable_host_notify(struct i801_priv *priv)
  869. {
  870. if (!(priv->features & FEATURE_HOST_NOTIFY))
  871. return;
  872. outb_p(priv->original_slvcmd, SMBSLVCMD(priv));
  873. }
  874. static const struct i2c_algorithm smbus_algorithm = {
  875. .smbus_xfer = i801_access,
  876. .functionality = i801_func,
  877. };
  878. static const struct pci_device_id i801_ids[] = {
  879. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
  880. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
  881. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
  882. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
  883. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
  884. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
  885. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
  886. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
  887. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
  888. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
  889. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
  890. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
  891. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
  892. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
  893. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
  894. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
  895. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
  896. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
  897. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
  898. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
  899. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
  900. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
  901. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
  902. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
  903. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
  904. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
  905. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
  906. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
  907. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
  908. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
  909. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
  910. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS) },
  911. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
  912. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
  913. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
  914. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
  915. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS) },
  916. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS) },
  917. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CDF_SMBUS) },
  918. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMBUS) },
  919. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROXTON_SMBUS) },
  920. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS) },
  921. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS) },
  922. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS) },
  923. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS) },
  924. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS) },
  925. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS) },
  926. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COMETLAKE_SMBUS) },
  927. { 0, }
  928. };
  929. MODULE_DEVICE_TABLE(pci, i801_ids);
  930. #if defined CONFIG_X86 && defined CONFIG_DMI
  931. static unsigned char apanel_addr;
  932. /* Scan the system ROM for the signature "FJKEYINF" */
  933. static __init const void __iomem *bios_signature(const void __iomem *bios)
  934. {
  935. ssize_t offset;
  936. const unsigned char signature[] = "FJKEYINF";
  937. for (offset = 0; offset < 0x10000; offset += 0x10) {
  938. if (check_signature(bios + offset, signature,
  939. sizeof(signature)-1))
  940. return bios + offset;
  941. }
  942. return NULL;
  943. }
  944. static void __init input_apanel_init(void)
  945. {
  946. void __iomem *bios;
  947. const void __iomem *p;
  948. bios = ioremap(0xF0000, 0x10000); /* Can't fail */
  949. p = bios_signature(bios);
  950. if (p) {
  951. /* just use the first address */
  952. apanel_addr = readb(p + 8 + 3) >> 1;
  953. }
  954. iounmap(bios);
  955. }
  956. struct dmi_onboard_device_info {
  957. const char *name;
  958. u8 type;
  959. unsigned short i2c_addr;
  960. const char *i2c_type;
  961. };
  962. static const struct dmi_onboard_device_info dmi_devices[] = {
  963. { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
  964. { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
  965. { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
  966. };
  967. static void dmi_check_onboard_device(u8 type, const char *name,
  968. struct i2c_adapter *adap)
  969. {
  970. int i;
  971. struct i2c_board_info info;
  972. for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
  973. /* & ~0x80, ignore enabled/disabled bit */
  974. if ((type & ~0x80) != dmi_devices[i].type)
  975. continue;
  976. if (strcasecmp(name, dmi_devices[i].name))
  977. continue;
  978. memset(&info, 0, sizeof(struct i2c_board_info));
  979. info.addr = dmi_devices[i].i2c_addr;
  980. strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
  981. i2c_new_device(adap, &info);
  982. break;
  983. }
  984. }
  985. /* We use our own function to check for onboard devices instead of
  986. dmi_find_device() as some buggy BIOS's have the devices we are interested
  987. in marked as disabled */
  988. static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
  989. {
  990. int i, count;
  991. if (dm->type != 10)
  992. return;
  993. count = (dm->length - sizeof(struct dmi_header)) / 2;
  994. for (i = 0; i < count; i++) {
  995. const u8 *d = (char *)(dm + 1) + (i * 2);
  996. const char *name = ((char *) dm) + dm->length;
  997. u8 type = d[0];
  998. u8 s = d[1];
  999. if (!s)
  1000. continue;
  1001. s--;
  1002. while (s > 0 && name[0]) {
  1003. name += strlen(name) + 1;
  1004. s--;
  1005. }
  1006. if (name[0] == 0) /* Bogus string reference */
  1007. continue;
  1008. dmi_check_onboard_device(type, name, adap);
  1009. }
  1010. }
  1011. /* Register optional slaves */
  1012. static void i801_probe_optional_slaves(struct i801_priv *priv)
  1013. {
  1014. /* Only register slaves on main SMBus channel */
  1015. if (priv->features & FEATURE_IDF)
  1016. return;
  1017. if (apanel_addr) {
  1018. struct i2c_board_info info;
  1019. memset(&info, 0, sizeof(struct i2c_board_info));
  1020. info.addr = apanel_addr;
  1021. strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
  1022. i2c_new_device(&priv->adapter, &info);
  1023. }
  1024. if (dmi_name_in_vendors("FUJITSU"))
  1025. dmi_walk(dmi_check_onboard_devices, &priv->adapter);
  1026. }
  1027. #else
  1028. static void __init input_apanel_init(void) {}
  1029. static void i801_probe_optional_slaves(struct i801_priv *priv) {}
  1030. #endif /* CONFIG_X86 && CONFIG_DMI */
  1031. #if IS_ENABLED(CONFIG_I2C_MUX_GPIO) && defined CONFIG_DMI
  1032. static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
  1033. .gpio_chip = "gpio_ich",
  1034. .values = { 0x02, 0x03 },
  1035. .n_values = 2,
  1036. .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
  1037. .gpios = { 52, 53 },
  1038. .n_gpios = 2,
  1039. };
  1040. static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
  1041. .gpio_chip = "gpio_ich",
  1042. .values = { 0x02, 0x03, 0x01 },
  1043. .n_values = 3,
  1044. .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
  1045. .gpios = { 52, 53 },
  1046. .n_gpios = 2,
  1047. };
  1048. static const struct dmi_system_id mux_dmi_table[] = {
  1049. {
  1050. .matches = {
  1051. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1052. DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
  1053. },
  1054. .driver_data = &i801_mux_config_asus_z8_d12,
  1055. },
  1056. {
  1057. .matches = {
  1058. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1059. DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
  1060. },
  1061. .driver_data = &i801_mux_config_asus_z8_d12,
  1062. },
  1063. {
  1064. .matches = {
  1065. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1066. DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
  1067. },
  1068. .driver_data = &i801_mux_config_asus_z8_d12,
  1069. },
  1070. {
  1071. .matches = {
  1072. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1073. DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
  1074. },
  1075. .driver_data = &i801_mux_config_asus_z8_d12,
  1076. },
  1077. {
  1078. .matches = {
  1079. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1080. DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
  1081. },
  1082. .driver_data = &i801_mux_config_asus_z8_d12,
  1083. },
  1084. {
  1085. .matches = {
  1086. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1087. DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
  1088. },
  1089. .driver_data = &i801_mux_config_asus_z8_d12,
  1090. },
  1091. {
  1092. .matches = {
  1093. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1094. DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
  1095. },
  1096. .driver_data = &i801_mux_config_asus_z8_d18,
  1097. },
  1098. {
  1099. .matches = {
  1100. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1101. DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
  1102. },
  1103. .driver_data = &i801_mux_config_asus_z8_d18,
  1104. },
  1105. {
  1106. .matches = {
  1107. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  1108. DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
  1109. },
  1110. .driver_data = &i801_mux_config_asus_z8_d12,
  1111. },
  1112. { }
  1113. };
  1114. /* Setup multiplexing if needed */
  1115. static int i801_add_mux(struct i801_priv *priv)
  1116. {
  1117. struct device *dev = &priv->adapter.dev;
  1118. const struct i801_mux_config *mux_config;
  1119. struct i2c_mux_gpio_platform_data gpio_data;
  1120. int err;
  1121. if (!priv->mux_drvdata)
  1122. return 0;
  1123. mux_config = priv->mux_drvdata;
  1124. /* Prepare the platform data */
  1125. memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
  1126. gpio_data.parent = priv->adapter.nr;
  1127. gpio_data.values = mux_config->values;
  1128. gpio_data.n_values = mux_config->n_values;
  1129. gpio_data.classes = mux_config->classes;
  1130. gpio_data.gpio_chip = mux_config->gpio_chip;
  1131. gpio_data.gpios = mux_config->gpios;
  1132. gpio_data.n_gpios = mux_config->n_gpios;
  1133. gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
  1134. /* Register the mux device */
  1135. priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
  1136. PLATFORM_DEVID_AUTO, &gpio_data,
  1137. sizeof(struct i2c_mux_gpio_platform_data));
  1138. if (IS_ERR(priv->mux_pdev)) {
  1139. err = PTR_ERR(priv->mux_pdev);
  1140. priv->mux_pdev = NULL;
  1141. dev_err(dev, "Failed to register i2c-mux-gpio device\n");
  1142. return err;
  1143. }
  1144. return 0;
  1145. }
  1146. static void i801_del_mux(struct i801_priv *priv)
  1147. {
  1148. if (priv->mux_pdev)
  1149. platform_device_unregister(priv->mux_pdev);
  1150. }
  1151. static unsigned int i801_get_adapter_class(struct i801_priv *priv)
  1152. {
  1153. const struct dmi_system_id *id;
  1154. const struct i801_mux_config *mux_config;
  1155. unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  1156. int i;
  1157. id = dmi_first_match(mux_dmi_table);
  1158. if (id) {
  1159. /* Remove branch classes from trunk */
  1160. mux_config = id->driver_data;
  1161. for (i = 0; i < mux_config->n_values; i++)
  1162. class &= ~mux_config->classes[i];
  1163. /* Remember for later */
  1164. priv->mux_drvdata = mux_config;
  1165. }
  1166. return class;
  1167. }
  1168. #else
  1169. static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
  1170. static inline void i801_del_mux(struct i801_priv *priv) { }
  1171. static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
  1172. {
  1173. return I2C_CLASS_HWMON | I2C_CLASS_SPD;
  1174. }
  1175. #endif
  1176. static const struct itco_wdt_platform_data tco_platform_data = {
  1177. .name = "Intel PCH",
  1178. .version = 4,
  1179. };
  1180. static DEFINE_SPINLOCK(p2sb_spinlock);
  1181. static void i801_add_tco(struct i801_priv *priv)
  1182. {
  1183. struct pci_dev *pci_dev = priv->pci_dev;
  1184. struct resource tco_res[3], *res;
  1185. struct platform_device *pdev;
  1186. unsigned int devfn;
  1187. u32 tco_base, tco_ctl;
  1188. u32 base_addr, ctrl_val;
  1189. u64 base64_addr;
  1190. u8 hidden;
  1191. if (!(priv->features & FEATURE_TCO))
  1192. return;
  1193. pci_read_config_dword(pci_dev, TCOBASE, &tco_base);
  1194. pci_read_config_dword(pci_dev, TCOCTL, &tco_ctl);
  1195. if (!(tco_ctl & TCOCTL_EN))
  1196. return;
  1197. memset(tco_res, 0, sizeof(tco_res));
  1198. res = &tco_res[ICH_RES_IO_TCO];
  1199. res->start = tco_base & ~1;
  1200. res->end = res->start + 32 - 1;
  1201. res->flags = IORESOURCE_IO;
  1202. /*
  1203. * Power Management registers.
  1204. */
  1205. devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 2);
  1206. pci_bus_read_config_dword(pci_dev->bus, devfn, ACPIBASE, &base_addr);
  1207. res = &tco_res[ICH_RES_IO_SMI];
  1208. res->start = (base_addr & ~1) + ACPIBASE_SMI_OFF;
  1209. res->end = res->start + 3;
  1210. res->flags = IORESOURCE_IO;
  1211. /*
  1212. * Enable the ACPI I/O space.
  1213. */
  1214. pci_bus_read_config_dword(pci_dev->bus, devfn, ACPICTRL, &ctrl_val);
  1215. ctrl_val |= ACPICTRL_EN;
  1216. pci_bus_write_config_dword(pci_dev->bus, devfn, ACPICTRL, ctrl_val);
  1217. /*
  1218. * We must access the NO_REBOOT bit over the Primary to Sideband
  1219. * bridge (P2SB). The BIOS prevents the P2SB device from being
  1220. * enumerated by the PCI subsystem, so we need to unhide/hide it
  1221. * to lookup the P2SB BAR.
  1222. */
  1223. spin_lock(&p2sb_spinlock);
  1224. devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 1);
  1225. /* Unhide the P2SB device, if it is hidden */
  1226. pci_bus_read_config_byte(pci_dev->bus, devfn, 0xe1, &hidden);
  1227. if (hidden)
  1228. pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, 0x0);
  1229. pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR, &base_addr);
  1230. base64_addr = base_addr & 0xfffffff0;
  1231. pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR + 0x4, &base_addr);
  1232. base64_addr |= (u64)base_addr << 32;
  1233. /* Hide the P2SB device, if it was hidden before */
  1234. if (hidden)
  1235. pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, hidden);
  1236. spin_unlock(&p2sb_spinlock);
  1237. res = &tco_res[ICH_RES_MEM_OFF];
  1238. if (pci_dev->device == PCI_DEVICE_ID_INTEL_DNV_SMBUS)
  1239. res->start = (resource_size_t)base64_addr + SBREG_SMBCTRL_DNV;
  1240. else
  1241. res->start = (resource_size_t)base64_addr + SBREG_SMBCTRL;
  1242. res->end = res->start + 3;
  1243. res->flags = IORESOURCE_MEM;
  1244. pdev = platform_device_register_resndata(&pci_dev->dev, "iTCO_wdt", -1,
  1245. tco_res, 3, &tco_platform_data,
  1246. sizeof(tco_platform_data));
  1247. if (IS_ERR(pdev)) {
  1248. dev_warn(&pci_dev->dev, "failed to create iTCO device\n");
  1249. return;
  1250. }
  1251. priv->tco_pdev = pdev;
  1252. }
  1253. #ifdef CONFIG_ACPI
  1254. static bool i801_acpi_is_smbus_ioport(const struct i801_priv *priv,
  1255. acpi_physical_address address)
  1256. {
  1257. return address >= priv->smba &&
  1258. address <= pci_resource_end(priv->pci_dev, SMBBAR);
  1259. }
  1260. static acpi_status
  1261. i801_acpi_io_handler(u32 function, acpi_physical_address address, u32 bits,
  1262. u64 *value, void *handler_context, void *region_context)
  1263. {
  1264. struct i801_priv *priv = handler_context;
  1265. struct pci_dev *pdev = priv->pci_dev;
  1266. acpi_status status;
  1267. /*
  1268. * Once BIOS AML code touches the OpRegion we warn and inhibit any
  1269. * further access from the driver itself. This device is now owned
  1270. * by the system firmware.
  1271. */
  1272. mutex_lock(&priv->acpi_lock);
  1273. if (!priv->acpi_reserved && i801_acpi_is_smbus_ioport(priv, address)) {
  1274. priv->acpi_reserved = true;
  1275. dev_warn(&pdev->dev, "BIOS is accessing SMBus registers\n");
  1276. dev_warn(&pdev->dev, "Driver SMBus register access inhibited\n");
  1277. /*
  1278. * BIOS is accessing the host controller so prevent it from
  1279. * suspending automatically from now on.
  1280. */
  1281. pm_runtime_get_sync(&pdev->dev);
  1282. }
  1283. if ((function & ACPI_IO_MASK) == ACPI_READ)
  1284. status = acpi_os_read_port(address, (u32 *)value, bits);
  1285. else
  1286. status = acpi_os_write_port(address, (u32)*value, bits);
  1287. mutex_unlock(&priv->acpi_lock);
  1288. return status;
  1289. }
  1290. static int i801_acpi_probe(struct i801_priv *priv)
  1291. {
  1292. struct acpi_device *adev;
  1293. acpi_status status;
  1294. adev = ACPI_COMPANION(&priv->pci_dev->dev);
  1295. if (adev) {
  1296. status = acpi_install_address_space_handler(adev->handle,
  1297. ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler,
  1298. NULL, priv);
  1299. if (ACPI_SUCCESS(status))
  1300. return 0;
  1301. }
  1302. return acpi_check_resource_conflict(&priv->pci_dev->resource[SMBBAR]);
  1303. }
  1304. static void i801_acpi_remove(struct i801_priv *priv)
  1305. {
  1306. struct acpi_device *adev;
  1307. adev = ACPI_COMPANION(&priv->pci_dev->dev);
  1308. if (!adev)
  1309. return;
  1310. acpi_remove_address_space_handler(adev->handle,
  1311. ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler);
  1312. mutex_lock(&priv->acpi_lock);
  1313. if (priv->acpi_reserved)
  1314. pm_runtime_put(&priv->pci_dev->dev);
  1315. mutex_unlock(&priv->acpi_lock);
  1316. }
  1317. #else
  1318. static inline int i801_acpi_probe(struct i801_priv *priv) { return 0; }
  1319. static inline void i801_acpi_remove(struct i801_priv *priv) { }
  1320. #endif
  1321. static unsigned char i801_setup_hstcfg(struct i801_priv *priv)
  1322. {
  1323. unsigned char hstcfg = priv->original_hstcfg;
  1324. hstcfg &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
  1325. hstcfg |= SMBHSTCFG_HST_EN;
  1326. pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hstcfg);
  1327. return hstcfg;
  1328. }
  1329. static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1330. {
  1331. unsigned char temp;
  1332. int err, i;
  1333. struct i801_priv *priv;
  1334. priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
  1335. if (!priv)
  1336. return -ENOMEM;
  1337. i2c_set_adapdata(&priv->adapter, priv);
  1338. priv->adapter.owner = THIS_MODULE;
  1339. priv->adapter.class = i801_get_adapter_class(priv);
  1340. priv->adapter.algo = &smbus_algorithm;
  1341. priv->adapter.dev.parent = &dev->dev;
  1342. ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&dev->dev));
  1343. priv->adapter.retries = 3;
  1344. mutex_init(&priv->acpi_lock);
  1345. priv->pci_dev = dev;
  1346. switch (dev->device) {
  1347. case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS:
  1348. case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS:
  1349. case PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS:
  1350. case PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS:
  1351. case PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS:
  1352. case PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS:
  1353. case PCI_DEVICE_ID_INTEL_CDF_SMBUS:
  1354. case PCI_DEVICE_ID_INTEL_DNV_SMBUS:
  1355. case PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS:
  1356. case PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS:
  1357. case PCI_DEVICE_ID_INTEL_COMETLAKE_SMBUS:
  1358. priv->features |= FEATURE_I2C_BLOCK_READ;
  1359. priv->features |= FEATURE_IRQ;
  1360. priv->features |= FEATURE_SMBUS_PEC;
  1361. priv->features |= FEATURE_BLOCK_BUFFER;
  1362. /* If we have ACPI based watchdog use that instead */
  1363. if (!acpi_has_watchdog())
  1364. priv->features |= FEATURE_TCO;
  1365. priv->features |= FEATURE_HOST_NOTIFY;
  1366. break;
  1367. case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
  1368. case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
  1369. case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
  1370. case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
  1371. case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
  1372. case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
  1373. priv->features |= FEATURE_IDF;
  1374. /* fall through */
  1375. default:
  1376. priv->features |= FEATURE_I2C_BLOCK_READ;
  1377. priv->features |= FEATURE_IRQ;
  1378. /* fall through */
  1379. case PCI_DEVICE_ID_INTEL_82801DB_3:
  1380. priv->features |= FEATURE_SMBUS_PEC;
  1381. priv->features |= FEATURE_BLOCK_BUFFER;
  1382. /* fall through */
  1383. case PCI_DEVICE_ID_INTEL_82801CA_3:
  1384. priv->features |= FEATURE_HOST_NOTIFY;
  1385. /* fall through */
  1386. case PCI_DEVICE_ID_INTEL_82801BA_2:
  1387. case PCI_DEVICE_ID_INTEL_82801AB_3:
  1388. case PCI_DEVICE_ID_INTEL_82801AA_3:
  1389. break;
  1390. }
  1391. /* Disable features on user request */
  1392. for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
  1393. if (priv->features & disable_features & (1 << i))
  1394. dev_notice(&dev->dev, "%s disabled by user\n",
  1395. i801_feature_names[i]);
  1396. }
  1397. priv->features &= ~disable_features;
  1398. err = pcim_enable_device(dev);
  1399. if (err) {
  1400. dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
  1401. err);
  1402. return err;
  1403. }
  1404. pcim_pin_device(dev);
  1405. /* Determine the address of the SMBus area */
  1406. priv->smba = pci_resource_start(dev, SMBBAR);
  1407. if (!priv->smba) {
  1408. dev_err(&dev->dev,
  1409. "SMBus base address uninitialized, upgrade BIOS\n");
  1410. return -ENODEV;
  1411. }
  1412. if (i801_acpi_probe(priv))
  1413. return -ENODEV;
  1414. err = pcim_iomap_regions(dev, 1 << SMBBAR,
  1415. dev_driver_string(&dev->dev));
  1416. if (err) {
  1417. dev_err(&dev->dev,
  1418. "Failed to request SMBus region 0x%lx-0x%Lx\n",
  1419. priv->smba,
  1420. (unsigned long long)pci_resource_end(dev, SMBBAR));
  1421. i801_acpi_remove(priv);
  1422. return err;
  1423. }
  1424. pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &priv->original_hstcfg);
  1425. temp = i801_setup_hstcfg(priv);
  1426. if (!(priv->original_hstcfg & SMBHSTCFG_HST_EN))
  1427. dev_info(&dev->dev, "Enabling SMBus device\n");
  1428. if (temp & SMBHSTCFG_SMB_SMI_EN) {
  1429. dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
  1430. /* Disable SMBus interrupt feature if SMBus using SMI# */
  1431. priv->features &= ~FEATURE_IRQ;
  1432. }
  1433. if (temp & SMBHSTCFG_SPD_WD)
  1434. dev_info(&dev->dev, "SPD Write Disable is set\n");
  1435. /* Clear special mode bits */
  1436. if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
  1437. outb_p(inb_p(SMBAUXCTL(priv)) &
  1438. ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
  1439. /* Remember original Host Notify setting */
  1440. if (priv->features & FEATURE_HOST_NOTIFY)
  1441. priv->original_slvcmd = inb_p(SMBSLVCMD(priv));
  1442. /* Default timeout in interrupt mode: 200 ms */
  1443. priv->adapter.timeout = HZ / 5;
  1444. if (dev->irq == IRQ_NOTCONNECTED)
  1445. priv->features &= ~FEATURE_IRQ;
  1446. if (priv->features & FEATURE_IRQ) {
  1447. u16 pcictl, pcists;
  1448. /* Complain if an interrupt is already pending */
  1449. pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
  1450. if (pcists & SMBPCISTS_INTS)
  1451. dev_warn(&dev->dev, "An interrupt is pending!\n");
  1452. /* Check if interrupts have been disabled */
  1453. pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
  1454. if (pcictl & SMBPCICTL_INTDIS) {
  1455. dev_info(&dev->dev, "Interrupts are disabled\n");
  1456. priv->features &= ~FEATURE_IRQ;
  1457. }
  1458. }
  1459. if (priv->features & FEATURE_IRQ) {
  1460. init_waitqueue_head(&priv->waitq);
  1461. err = devm_request_irq(&dev->dev, dev->irq, i801_isr,
  1462. IRQF_SHARED,
  1463. dev_driver_string(&dev->dev), priv);
  1464. if (err) {
  1465. dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
  1466. dev->irq, err);
  1467. priv->features &= ~FEATURE_IRQ;
  1468. }
  1469. }
  1470. dev_info(&dev->dev, "SMBus using %s\n",
  1471. priv->features & FEATURE_IRQ ? "PCI interrupt" : "polling");
  1472. i801_add_tco(priv);
  1473. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  1474. "SMBus I801 adapter at %04lx", priv->smba);
  1475. err = i2c_add_adapter(&priv->adapter);
  1476. if (err) {
  1477. i801_acpi_remove(priv);
  1478. return err;
  1479. }
  1480. i801_enable_host_notify(&priv->adapter);
  1481. i801_probe_optional_slaves(priv);
  1482. /* We ignore errors - multiplexing is optional */
  1483. i801_add_mux(priv);
  1484. pci_set_drvdata(dev, priv);
  1485. dev_pm_set_driver_flags(&dev->dev, DPM_FLAG_NEVER_SKIP);
  1486. pm_runtime_set_autosuspend_delay(&dev->dev, 1000);
  1487. pm_runtime_use_autosuspend(&dev->dev);
  1488. pm_runtime_put_autosuspend(&dev->dev);
  1489. pm_runtime_allow(&dev->dev);
  1490. return 0;
  1491. }
  1492. static void i801_remove(struct pci_dev *dev)
  1493. {
  1494. struct i801_priv *priv = pci_get_drvdata(dev);
  1495. pm_runtime_forbid(&dev->dev);
  1496. pm_runtime_get_noresume(&dev->dev);
  1497. i801_disable_host_notify(priv);
  1498. i801_del_mux(priv);
  1499. i2c_del_adapter(&priv->adapter);
  1500. i801_acpi_remove(priv);
  1501. pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
  1502. platform_device_unregister(priv->tco_pdev);
  1503. /*
  1504. * do not call pci_disable_device(dev) since it can cause hard hangs on
  1505. * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
  1506. */
  1507. }
  1508. static void i801_shutdown(struct pci_dev *dev)
  1509. {
  1510. struct i801_priv *priv = pci_get_drvdata(dev);
  1511. /* Restore config registers to avoid hard hang on some systems */
  1512. i801_disable_host_notify(priv);
  1513. pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
  1514. }
  1515. #ifdef CONFIG_PM_SLEEP
  1516. static int i801_suspend(struct device *dev)
  1517. {
  1518. struct pci_dev *pci_dev = to_pci_dev(dev);
  1519. struct i801_priv *priv = pci_get_drvdata(pci_dev);
  1520. pci_write_config_byte(pci_dev, SMBHSTCFG, priv->original_hstcfg);
  1521. return 0;
  1522. }
  1523. static int i801_resume(struct device *dev)
  1524. {
  1525. struct pci_dev *pci_dev = to_pci_dev(dev);
  1526. struct i801_priv *priv = pci_get_drvdata(pci_dev);
  1527. i801_setup_hstcfg(priv);
  1528. i801_enable_host_notify(&priv->adapter);
  1529. return 0;
  1530. }
  1531. #endif
  1532. static SIMPLE_DEV_PM_OPS(i801_pm_ops, i801_suspend, i801_resume);
  1533. static struct pci_driver i801_driver = {
  1534. .name = "i801_smbus",
  1535. .id_table = i801_ids,
  1536. .probe = i801_probe,
  1537. .remove = i801_remove,
  1538. .shutdown = i801_shutdown,
  1539. .driver = {
  1540. .pm = &i801_pm_ops,
  1541. },
  1542. };
  1543. static int __init i2c_i801_init(void)
  1544. {
  1545. if (dmi_name_in_vendors("FUJITSU"))
  1546. input_apanel_init();
  1547. return pci_register_driver(&i801_driver);
  1548. }
  1549. static void __exit i2c_i801_exit(void)
  1550. {
  1551. pci_unregister_driver(&i801_driver);
  1552. }
  1553. MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, Jean Delvare <jdelvare@suse.de>");
  1554. MODULE_DESCRIPTION("I801 SMBus driver");
  1555. MODULE_LICENSE("GPL");
  1556. module_init(i2c_i801_init);
  1557. module_exit(i2c_i801_exit);