i2c-i801.c 50 KB

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