i2c-i801.c 50 KB

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