i2c-piix4.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
  3. Philip Edelbrock <phil@netroedge.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. */
  13. /*
  14. Supports:
  15. Intel PIIX4, 440MX
  16. Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
  17. ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
  18. AMD Hudson-2, ML, CZ
  19. SMSC Victory66
  20. Note: we assume there can only be one device, with one or more
  21. SMBus interfaces.
  22. The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS).
  23. For devices supporting multiple ports the i2c_adapter should provide
  24. an i2c_algorithm to access them.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/pci.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/stddef.h>
  32. #include <linux/ioport.h>
  33. #include <linux/i2c.h>
  34. #include <linux/slab.h>
  35. #include <linux/dmi.h>
  36. #include <linux/acpi.h>
  37. #include <linux/io.h>
  38. /* PIIX4 SMBus address offsets */
  39. #define SMBHSTSTS (0 + piix4_smba)
  40. #define SMBHSLVSTS (1 + piix4_smba)
  41. #define SMBHSTCNT (2 + piix4_smba)
  42. #define SMBHSTCMD (3 + piix4_smba)
  43. #define SMBHSTADD (4 + piix4_smba)
  44. #define SMBHSTDAT0 (5 + piix4_smba)
  45. #define SMBHSTDAT1 (6 + piix4_smba)
  46. #define SMBBLKDAT (7 + piix4_smba)
  47. #define SMBSLVCNT (8 + piix4_smba)
  48. #define SMBSHDWCMD (9 + piix4_smba)
  49. #define SMBSLVEVT (0xA + piix4_smba)
  50. #define SMBSLVDAT (0xC + piix4_smba)
  51. /* count for request_region */
  52. #define SMBIOSIZE 9
  53. /* PCI Address Constants */
  54. #define SMBBA 0x090
  55. #define SMBHSTCFG 0x0D2
  56. #define SMBSLVC 0x0D3
  57. #define SMBSHDW1 0x0D4
  58. #define SMBSHDW2 0x0D5
  59. #define SMBREV 0x0D6
  60. /* Other settings */
  61. #define MAX_TIMEOUT 500
  62. #define ENABLE_INT9 0
  63. /* PIIX4 constants */
  64. #define PIIX4_QUICK 0x00
  65. #define PIIX4_BYTE 0x04
  66. #define PIIX4_BYTE_DATA 0x08
  67. #define PIIX4_WORD_DATA 0x0C
  68. #define PIIX4_BLOCK_DATA 0x14
  69. /* Multi-port constants */
  70. #define PIIX4_MAX_ADAPTERS 4
  71. /* SB800 constants */
  72. #define SB800_PIIX4_SMB_IDX 0xcd6
  73. #define KERNCZ_IMC_IDX 0x3e
  74. #define KERNCZ_IMC_DATA 0x3f
  75. /*
  76. * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
  77. * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
  78. * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
  79. */
  80. #define SB800_PIIX4_PORT_IDX 0x2c
  81. #define SB800_PIIX4_PORT_IDX_ALT 0x2e
  82. #define SB800_PIIX4_PORT_IDX_SEL 0x2f
  83. #define SB800_PIIX4_PORT_IDX_MASK 0x06
  84. #define SB800_PIIX4_PORT_IDX_SHIFT 1
  85. /* On kerncz and Hudson2, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
  86. #define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
  87. #define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
  88. #define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
  89. /* insmod parameters */
  90. /* If force is set to anything different from 0, we forcibly enable the
  91. PIIX4. DANGEROUS! */
  92. static int force;
  93. module_param (force, int, 0);
  94. MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
  95. /* If force_addr is set to anything different from 0, we forcibly enable
  96. the PIIX4 at the given address. VERY DANGEROUS! */
  97. static int force_addr;
  98. module_param_hw(force_addr, int, ioport, 0);
  99. MODULE_PARM_DESC(force_addr,
  100. "Forcibly enable the PIIX4 at the given address. "
  101. "EXTREMELY DANGEROUS!");
  102. static int srvrworks_csb5_delay;
  103. static struct pci_driver piix4_driver;
  104. static const struct dmi_system_id piix4_dmi_blacklist[] = {
  105. {
  106. .ident = "Sapphire AM2RD790",
  107. .matches = {
  108. DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
  109. DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
  110. },
  111. },
  112. {
  113. .ident = "DFI Lanparty UT 790FX",
  114. .matches = {
  115. DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
  116. DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
  117. },
  118. },
  119. { }
  120. };
  121. /* The IBM entry is in a separate table because we only check it
  122. on Intel-based systems */
  123. static const struct dmi_system_id piix4_dmi_ibm[] = {
  124. {
  125. .ident = "IBM",
  126. .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
  127. },
  128. { },
  129. };
  130. /*
  131. * SB800 globals
  132. */
  133. static u8 piix4_port_sel_sb800;
  134. static u8 piix4_port_mask_sb800;
  135. static u8 piix4_port_shift_sb800;
  136. static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
  137. " port 0", " port 2", " port 3", " port 4"
  138. };
  139. static const char *piix4_aux_port_name_sb800 = " port 1";
  140. struct i2c_piix4_adapdata {
  141. unsigned short smba;
  142. /* SB800 */
  143. bool sb800_main;
  144. bool notify_imc;
  145. u8 port; /* Port number, shifted */
  146. };
  147. static int piix4_setup(struct pci_dev *PIIX4_dev,
  148. const struct pci_device_id *id)
  149. {
  150. unsigned char temp;
  151. unsigned short piix4_smba;
  152. if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
  153. (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
  154. srvrworks_csb5_delay = 1;
  155. /* On some motherboards, it was reported that accessing the SMBus
  156. caused severe hardware problems */
  157. if (dmi_check_system(piix4_dmi_blacklist)) {
  158. dev_err(&PIIX4_dev->dev,
  159. "Accessing the SMBus on this system is unsafe!\n");
  160. return -EPERM;
  161. }
  162. /* Don't access SMBus on IBM systems which get corrupted eeproms */
  163. if (dmi_check_system(piix4_dmi_ibm) &&
  164. PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
  165. dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
  166. "may corrupt your serial eeprom! Refusing to load "
  167. "module!\n");
  168. return -EPERM;
  169. }
  170. /* Determine the address of the SMBus areas */
  171. if (force_addr) {
  172. piix4_smba = force_addr & 0xfff0;
  173. force = 0;
  174. } else {
  175. pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
  176. piix4_smba &= 0xfff0;
  177. if(piix4_smba == 0) {
  178. dev_err(&PIIX4_dev->dev, "SMBus base address "
  179. "uninitialized - upgrade BIOS or use "
  180. "force_addr=0xaddr\n");
  181. return -ENODEV;
  182. }
  183. }
  184. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  185. return -ENODEV;
  186. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  187. dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
  188. piix4_smba);
  189. return -EBUSY;
  190. }
  191. pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
  192. /* If force_addr is set, we program the new address here. Just to make
  193. sure, we disable the PIIX4 first. */
  194. if (force_addr) {
  195. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
  196. pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
  197. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
  198. dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
  199. "new address %04x!\n", piix4_smba);
  200. } else if ((temp & 1) == 0) {
  201. if (force) {
  202. /* This should never need to be done, but has been
  203. * noted that many Dell machines have the SMBus
  204. * interface on the PIIX4 disabled!? NOTE: This assumes
  205. * I/O space and other allocations WERE done by the
  206. * Bios! Don't complain if your hardware does weird
  207. * things after enabling this. :') Check for Bios
  208. * updates before resorting to this.
  209. */
  210. pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
  211. temp | 1);
  212. dev_notice(&PIIX4_dev->dev,
  213. "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
  214. } else {
  215. dev_err(&PIIX4_dev->dev,
  216. "SMBus Host Controller not enabled!\n");
  217. release_region(piix4_smba, SMBIOSIZE);
  218. return -ENODEV;
  219. }
  220. }
  221. if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
  222. dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
  223. else if ((temp & 0x0E) == 0)
  224. dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
  225. else
  226. dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
  227. "(or code out of date)!\n");
  228. pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
  229. dev_info(&PIIX4_dev->dev,
  230. "SMBus Host Controller at 0x%x, revision %d\n",
  231. piix4_smba, temp);
  232. return piix4_smba;
  233. }
  234. static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
  235. const struct pci_device_id *id, u8 aux)
  236. {
  237. unsigned short piix4_smba;
  238. u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
  239. u8 i2ccfg, i2ccfg_offset = 0x10;
  240. /* SB800 and later SMBus does not support forcing address */
  241. if (force || force_addr) {
  242. dev_err(&PIIX4_dev->dev, "SMBus does not support "
  243. "forcing address!\n");
  244. return -EINVAL;
  245. }
  246. /* Determine the address of the SMBus areas */
  247. if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
  248. PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
  249. PIIX4_dev->revision >= 0x41) ||
  250. (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
  251. PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
  252. PIIX4_dev->revision >= 0x49))
  253. smb_en = 0x00;
  254. else
  255. smb_en = (aux) ? 0x28 : 0x2c;
  256. if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb")) {
  257. dev_err(&PIIX4_dev->dev,
  258. "SMB base address index region 0x%x already in use.\n",
  259. SB800_PIIX4_SMB_IDX);
  260. return -EBUSY;
  261. }
  262. outb_p(smb_en, SB800_PIIX4_SMB_IDX);
  263. smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
  264. outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
  265. smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
  266. release_region(SB800_PIIX4_SMB_IDX, 2);
  267. if (!smb_en) {
  268. smb_en_status = smba_en_lo & 0x10;
  269. piix4_smba = smba_en_hi << 8;
  270. if (aux)
  271. piix4_smba |= 0x20;
  272. } else {
  273. smb_en_status = smba_en_lo & 0x01;
  274. piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
  275. }
  276. if (!smb_en_status) {
  277. dev_err(&PIIX4_dev->dev,
  278. "SMBus Host Controller not enabled!\n");
  279. return -ENODEV;
  280. }
  281. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  282. return -ENODEV;
  283. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  284. dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
  285. piix4_smba);
  286. return -EBUSY;
  287. }
  288. /* Aux SMBus does not support IRQ information */
  289. if (aux) {
  290. dev_info(&PIIX4_dev->dev,
  291. "Auxiliary SMBus Host Controller at 0x%x\n",
  292. piix4_smba);
  293. return piix4_smba;
  294. }
  295. /* Request the SMBus I2C bus config region */
  296. if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
  297. dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
  298. "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
  299. release_region(piix4_smba, SMBIOSIZE);
  300. return -EBUSY;
  301. }
  302. i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
  303. release_region(piix4_smba + i2ccfg_offset, 1);
  304. if (i2ccfg & 1)
  305. dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
  306. else
  307. dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
  308. dev_info(&PIIX4_dev->dev,
  309. "SMBus Host Controller at 0x%x, revision %d\n",
  310. piix4_smba, i2ccfg >> 4);
  311. /* Find which register is used for port selection */
  312. if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) {
  313. if (PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS ||
  314. (PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
  315. PIIX4_dev->revision >= 0x1F)) {
  316. piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
  317. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
  318. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
  319. } else {
  320. piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
  321. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
  322. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
  323. }
  324. } else {
  325. if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2,
  326. "sb800_piix4_smb")) {
  327. release_region(piix4_smba, SMBIOSIZE);
  328. return -EBUSY;
  329. }
  330. outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
  331. port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
  332. piix4_port_sel_sb800 = (port_sel & 0x01) ?
  333. SB800_PIIX4_PORT_IDX_ALT :
  334. SB800_PIIX4_PORT_IDX;
  335. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
  336. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
  337. release_region(SB800_PIIX4_SMB_IDX, 2);
  338. }
  339. dev_info(&PIIX4_dev->dev,
  340. "Using register 0x%02x for SMBus port selection\n",
  341. (unsigned int)piix4_port_sel_sb800);
  342. return piix4_smba;
  343. }
  344. static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
  345. const struct pci_device_id *id,
  346. unsigned short base_reg_addr)
  347. {
  348. /* Set up auxiliary SMBus controllers found on some
  349. * AMD chipsets e.g. SP5100 (SB700 derivative) */
  350. unsigned short piix4_smba;
  351. /* Read address of auxiliary SMBus controller */
  352. pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
  353. if ((piix4_smba & 1) == 0) {
  354. dev_dbg(&PIIX4_dev->dev,
  355. "Auxiliary SMBus controller not enabled\n");
  356. return -ENODEV;
  357. }
  358. piix4_smba &= 0xfff0;
  359. if (piix4_smba == 0) {
  360. dev_dbg(&PIIX4_dev->dev,
  361. "Auxiliary SMBus base address uninitialized\n");
  362. return -ENODEV;
  363. }
  364. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  365. return -ENODEV;
  366. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  367. dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
  368. "already in use!\n", piix4_smba);
  369. return -EBUSY;
  370. }
  371. dev_info(&PIIX4_dev->dev,
  372. "Auxiliary SMBus Host Controller at 0x%x\n",
  373. piix4_smba);
  374. return piix4_smba;
  375. }
  376. static int piix4_transaction(struct i2c_adapter *piix4_adapter)
  377. {
  378. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
  379. unsigned short piix4_smba = adapdata->smba;
  380. int temp;
  381. int result = 0;
  382. int timeout = 0;
  383. dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
  384. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  385. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  386. inb_p(SMBHSTDAT1));
  387. /* Make sure the SMBus host is ready to start transmitting */
  388. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  389. dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
  390. "Resetting...\n", temp);
  391. outb_p(temp, SMBHSTSTS);
  392. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  393. dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
  394. return -EBUSY;
  395. } else {
  396. dev_dbg(&piix4_adapter->dev, "Successful!\n");
  397. }
  398. }
  399. /* start the transaction by setting bit 6 */
  400. outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
  401. /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
  402. if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
  403. usleep_range(2000, 2100);
  404. else
  405. usleep_range(250, 500);
  406. while ((++timeout < MAX_TIMEOUT) &&
  407. ((temp = inb_p(SMBHSTSTS)) & 0x01))
  408. usleep_range(250, 500);
  409. /* If the SMBus is still busy, we give up */
  410. if (timeout == MAX_TIMEOUT) {
  411. dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
  412. result = -ETIMEDOUT;
  413. }
  414. if (temp & 0x10) {
  415. result = -EIO;
  416. dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
  417. }
  418. if (temp & 0x08) {
  419. result = -EIO;
  420. dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
  421. "locked until next hard reset. (sorry!)\n");
  422. /* Clock stops and slave is stuck in mid-transmission */
  423. }
  424. if (temp & 0x04) {
  425. result = -ENXIO;
  426. dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
  427. }
  428. if (inb_p(SMBHSTSTS) != 0x00)
  429. outb_p(inb(SMBHSTSTS), SMBHSTSTS);
  430. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  431. dev_err(&piix4_adapter->dev, "Failed reset at end of "
  432. "transaction (%02x)\n", temp);
  433. }
  434. dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
  435. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  436. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  437. inb_p(SMBHSTDAT1));
  438. return result;
  439. }
  440. /* Return negative errno on error. */
  441. static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
  442. unsigned short flags, char read_write,
  443. u8 command, int size, union i2c_smbus_data * data)
  444. {
  445. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  446. unsigned short piix4_smba = adapdata->smba;
  447. int i, len;
  448. int status;
  449. switch (size) {
  450. case I2C_SMBUS_QUICK:
  451. outb_p((addr << 1) | read_write,
  452. SMBHSTADD);
  453. size = PIIX4_QUICK;
  454. break;
  455. case I2C_SMBUS_BYTE:
  456. outb_p((addr << 1) | read_write,
  457. SMBHSTADD);
  458. if (read_write == I2C_SMBUS_WRITE)
  459. outb_p(command, SMBHSTCMD);
  460. size = PIIX4_BYTE;
  461. break;
  462. case I2C_SMBUS_BYTE_DATA:
  463. outb_p((addr << 1) | read_write,
  464. SMBHSTADD);
  465. outb_p(command, SMBHSTCMD);
  466. if (read_write == I2C_SMBUS_WRITE)
  467. outb_p(data->byte, SMBHSTDAT0);
  468. size = PIIX4_BYTE_DATA;
  469. break;
  470. case I2C_SMBUS_WORD_DATA:
  471. outb_p((addr << 1) | read_write,
  472. SMBHSTADD);
  473. outb_p(command, SMBHSTCMD);
  474. if (read_write == I2C_SMBUS_WRITE) {
  475. outb_p(data->word & 0xff, SMBHSTDAT0);
  476. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  477. }
  478. size = PIIX4_WORD_DATA;
  479. break;
  480. case I2C_SMBUS_BLOCK_DATA:
  481. outb_p((addr << 1) | read_write,
  482. SMBHSTADD);
  483. outb_p(command, SMBHSTCMD);
  484. if (read_write == I2C_SMBUS_WRITE) {
  485. len = data->block[0];
  486. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  487. return -EINVAL;
  488. outb_p(len, SMBHSTDAT0);
  489. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  490. for (i = 1; i <= len; i++)
  491. outb_p(data->block[i], SMBBLKDAT);
  492. }
  493. size = PIIX4_BLOCK_DATA;
  494. break;
  495. default:
  496. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  497. return -EOPNOTSUPP;
  498. }
  499. outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
  500. status = piix4_transaction(adap);
  501. if (status)
  502. return status;
  503. if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
  504. return 0;
  505. switch (size) {
  506. case PIIX4_BYTE:
  507. case PIIX4_BYTE_DATA:
  508. data->byte = inb_p(SMBHSTDAT0);
  509. break;
  510. case PIIX4_WORD_DATA:
  511. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  512. break;
  513. case PIIX4_BLOCK_DATA:
  514. data->block[0] = inb_p(SMBHSTDAT0);
  515. if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
  516. return -EPROTO;
  517. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  518. for (i = 1; i <= data->block[0]; i++)
  519. data->block[i] = inb_p(SMBBLKDAT);
  520. break;
  521. }
  522. return 0;
  523. }
  524. static uint8_t piix4_imc_read(uint8_t idx)
  525. {
  526. outb_p(idx, KERNCZ_IMC_IDX);
  527. return inb_p(KERNCZ_IMC_DATA);
  528. }
  529. static void piix4_imc_write(uint8_t idx, uint8_t value)
  530. {
  531. outb_p(idx, KERNCZ_IMC_IDX);
  532. outb_p(value, KERNCZ_IMC_DATA);
  533. }
  534. static int piix4_imc_sleep(void)
  535. {
  536. int timeout = MAX_TIMEOUT;
  537. if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
  538. return -EBUSY;
  539. /* clear response register */
  540. piix4_imc_write(0x82, 0x00);
  541. /* request ownership flag */
  542. piix4_imc_write(0x83, 0xB4);
  543. /* kick off IMC Mailbox command 96 */
  544. piix4_imc_write(0x80, 0x96);
  545. while (timeout--) {
  546. if (piix4_imc_read(0x82) == 0xfa) {
  547. release_region(KERNCZ_IMC_IDX, 2);
  548. return 0;
  549. }
  550. usleep_range(1000, 2000);
  551. }
  552. release_region(KERNCZ_IMC_IDX, 2);
  553. return -ETIMEDOUT;
  554. }
  555. static void piix4_imc_wakeup(void)
  556. {
  557. int timeout = MAX_TIMEOUT;
  558. if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
  559. return;
  560. /* clear response register */
  561. piix4_imc_write(0x82, 0x00);
  562. /* release ownership flag */
  563. piix4_imc_write(0x83, 0xB5);
  564. /* kick off IMC Mailbox command 96 */
  565. piix4_imc_write(0x80, 0x96);
  566. while (timeout--) {
  567. if (piix4_imc_read(0x82) == 0xfa)
  568. break;
  569. usleep_range(1000, 2000);
  570. }
  571. release_region(KERNCZ_IMC_IDX, 2);
  572. }
  573. /*
  574. * Handles access to multiple SMBus ports on the SB800.
  575. * The port is selected by bits 2:1 of the smb_en register (0x2c).
  576. * Returns negative errno on error.
  577. *
  578. * Note: The selected port must be returned to the initial selection to avoid
  579. * problems on certain systems.
  580. */
  581. static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
  582. unsigned short flags, char read_write,
  583. u8 command, int size, union i2c_smbus_data *data)
  584. {
  585. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  586. unsigned short piix4_smba = adapdata->smba;
  587. int retries = MAX_TIMEOUT;
  588. int smbslvcnt;
  589. u8 smba_en_lo;
  590. u8 port;
  591. int retval;
  592. if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb"))
  593. return -EBUSY;
  594. /* Request the SMBUS semaphore, avoid conflicts with the IMC */
  595. smbslvcnt = inb_p(SMBSLVCNT);
  596. do {
  597. outb_p(smbslvcnt | 0x10, SMBSLVCNT);
  598. /* Check the semaphore status */
  599. smbslvcnt = inb_p(SMBSLVCNT);
  600. if (smbslvcnt & 0x10)
  601. break;
  602. usleep_range(1000, 2000);
  603. } while (--retries);
  604. /* SMBus is still owned by the IMC, we give up */
  605. if (!retries) {
  606. retval = -EBUSY;
  607. goto release;
  608. }
  609. /*
  610. * Notify the IMC (Integrated Micro Controller) if required.
  611. * Among other responsibilities, the IMC is in charge of monitoring
  612. * the System fans and temperature sensors, and act accordingly.
  613. * All this is done through SMBus and can/will collide
  614. * with our transactions if they are long (BLOCK_DATA).
  615. * Therefore we need to request the ownership flag during those
  616. * transactions.
  617. */
  618. if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc) {
  619. int ret;
  620. ret = piix4_imc_sleep();
  621. switch (ret) {
  622. case -EBUSY:
  623. dev_warn(&adap->dev,
  624. "IMC base address index region 0x%x already in use.\n",
  625. KERNCZ_IMC_IDX);
  626. break;
  627. case -ETIMEDOUT:
  628. dev_warn(&adap->dev,
  629. "Failed to communicate with the IMC.\n");
  630. break;
  631. default:
  632. break;
  633. }
  634. /* If IMC communication fails do not retry */
  635. if (ret) {
  636. dev_warn(&adap->dev,
  637. "Continuing without IMC notification.\n");
  638. adapdata->notify_imc = false;
  639. }
  640. }
  641. outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
  642. smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
  643. port = adapdata->port;
  644. if ((smba_en_lo & piix4_port_mask_sb800) != port)
  645. outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
  646. SB800_PIIX4_SMB_IDX + 1);
  647. retval = piix4_access(adap, addr, flags, read_write,
  648. command, size, data);
  649. outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
  650. /* Release the semaphore */
  651. outb_p(smbslvcnt | 0x20, SMBSLVCNT);
  652. if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc)
  653. piix4_imc_wakeup();
  654. release:
  655. release_region(SB800_PIIX4_SMB_IDX, 2);
  656. return retval;
  657. }
  658. static u32 piix4_func(struct i2c_adapter *adapter)
  659. {
  660. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  661. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  662. I2C_FUNC_SMBUS_BLOCK_DATA;
  663. }
  664. static const struct i2c_algorithm smbus_algorithm = {
  665. .smbus_xfer = piix4_access,
  666. .functionality = piix4_func,
  667. };
  668. static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
  669. .smbus_xfer = piix4_access_sb800,
  670. .functionality = piix4_func,
  671. };
  672. static const struct pci_device_id piix4_ids[] = {
  673. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
  674. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
  675. { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
  676. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
  677. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
  678. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
  679. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
  680. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
  681. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
  682. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  683. PCI_DEVICE_ID_SERVERWORKS_OSB4) },
  684. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  685. PCI_DEVICE_ID_SERVERWORKS_CSB5) },
  686. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  687. PCI_DEVICE_ID_SERVERWORKS_CSB6) },
  688. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  689. PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
  690. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  691. PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
  692. { 0, }
  693. };
  694. MODULE_DEVICE_TABLE (pci, piix4_ids);
  695. static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
  696. static struct i2c_adapter *piix4_aux_adapter;
  697. static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
  698. bool sb800_main, u8 port, bool notify_imc,
  699. const char *name, struct i2c_adapter **padap)
  700. {
  701. struct i2c_adapter *adap;
  702. struct i2c_piix4_adapdata *adapdata;
  703. int retval;
  704. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  705. if (adap == NULL) {
  706. release_region(smba, SMBIOSIZE);
  707. return -ENOMEM;
  708. }
  709. adap->owner = THIS_MODULE;
  710. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  711. adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
  712. : &smbus_algorithm;
  713. adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
  714. if (adapdata == NULL) {
  715. kfree(adap);
  716. release_region(smba, SMBIOSIZE);
  717. return -ENOMEM;
  718. }
  719. adapdata->smba = smba;
  720. adapdata->sb800_main = sb800_main;
  721. adapdata->port = port << piix4_port_shift_sb800;
  722. adapdata->notify_imc = notify_imc;
  723. /* set up the sysfs linkage to our parent device */
  724. adap->dev.parent = &dev->dev;
  725. snprintf(adap->name, sizeof(adap->name),
  726. "SMBus PIIX4 adapter%s at %04x", name, smba);
  727. i2c_set_adapdata(adap, adapdata);
  728. retval = i2c_add_adapter(adap);
  729. if (retval) {
  730. kfree(adapdata);
  731. kfree(adap);
  732. release_region(smba, SMBIOSIZE);
  733. return retval;
  734. }
  735. *padap = adap;
  736. return 0;
  737. }
  738. static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba,
  739. bool notify_imc)
  740. {
  741. struct i2c_piix4_adapdata *adapdata;
  742. int port;
  743. int retval;
  744. for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
  745. retval = piix4_add_adapter(dev, smba, true, port, notify_imc,
  746. piix4_main_port_names_sb800[port],
  747. &piix4_main_adapters[port]);
  748. if (retval < 0)
  749. goto error;
  750. }
  751. return retval;
  752. error:
  753. dev_err(&dev->dev,
  754. "Error setting up SB800 adapters. Unregistering!\n");
  755. while (--port >= 0) {
  756. adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
  757. if (adapdata->smba) {
  758. i2c_del_adapter(piix4_main_adapters[port]);
  759. kfree(adapdata);
  760. kfree(piix4_main_adapters[port]);
  761. piix4_main_adapters[port] = NULL;
  762. }
  763. }
  764. return retval;
  765. }
  766. static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
  767. {
  768. int retval;
  769. bool is_sb800 = false;
  770. if ((dev->vendor == PCI_VENDOR_ID_ATI &&
  771. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
  772. dev->revision >= 0x40) ||
  773. dev->vendor == PCI_VENDOR_ID_AMD) {
  774. bool notify_imc = false;
  775. is_sb800 = true;
  776. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  777. dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) {
  778. u8 imc;
  779. /*
  780. * Detect if IMC is active or not, this method is
  781. * described on coreboot's AMD IMC notes
  782. */
  783. pci_bus_read_config_byte(dev->bus, PCI_DEVFN(0x14, 3),
  784. 0x40, &imc);
  785. if (imc & 0x80)
  786. notify_imc = true;
  787. }
  788. /* base address location etc changed in SB800 */
  789. retval = piix4_setup_sb800(dev, id, 0);
  790. if (retval < 0)
  791. return retval;
  792. /*
  793. * Try to register multiplexed main SMBus adapter,
  794. * give up if we can't
  795. */
  796. retval = piix4_add_adapters_sb800(dev, retval, notify_imc);
  797. if (retval < 0)
  798. return retval;
  799. } else {
  800. retval = piix4_setup(dev, id);
  801. if (retval < 0)
  802. return retval;
  803. /* Try to register main SMBus adapter, give up if we can't */
  804. retval = piix4_add_adapter(dev, retval, false, 0, false, "",
  805. &piix4_main_adapters[0]);
  806. if (retval < 0)
  807. return retval;
  808. }
  809. /* Check for auxiliary SMBus on some AMD chipsets */
  810. retval = -ENODEV;
  811. if (dev->vendor == PCI_VENDOR_ID_ATI &&
  812. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
  813. if (dev->revision < 0x40) {
  814. retval = piix4_setup_aux(dev, id, 0x58);
  815. } else {
  816. /* SB800 added aux bus too */
  817. retval = piix4_setup_sb800(dev, id, 1);
  818. }
  819. }
  820. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  821. (dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS ||
  822. dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS)) {
  823. retval = piix4_setup_sb800(dev, id, 1);
  824. }
  825. if (retval > 0) {
  826. /* Try to add the aux adapter if it exists,
  827. * piix4_add_adapter will clean up if this fails */
  828. piix4_add_adapter(dev, retval, false, 0, false,
  829. is_sb800 ? piix4_aux_port_name_sb800 : "",
  830. &piix4_aux_adapter);
  831. }
  832. return 0;
  833. }
  834. static void piix4_adap_remove(struct i2c_adapter *adap)
  835. {
  836. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  837. if (adapdata->smba) {
  838. i2c_del_adapter(adap);
  839. if (adapdata->port == (0 << piix4_port_shift_sb800))
  840. release_region(adapdata->smba, SMBIOSIZE);
  841. kfree(adapdata);
  842. kfree(adap);
  843. }
  844. }
  845. static void piix4_remove(struct pci_dev *dev)
  846. {
  847. int port = PIIX4_MAX_ADAPTERS;
  848. while (--port >= 0) {
  849. if (piix4_main_adapters[port]) {
  850. piix4_adap_remove(piix4_main_adapters[port]);
  851. piix4_main_adapters[port] = NULL;
  852. }
  853. }
  854. if (piix4_aux_adapter) {
  855. piix4_adap_remove(piix4_aux_adapter);
  856. piix4_aux_adapter = NULL;
  857. }
  858. }
  859. static struct pci_driver piix4_driver = {
  860. .name = "piix4_smbus",
  861. .id_table = piix4_ids,
  862. .probe = piix4_probe,
  863. .remove = piix4_remove,
  864. };
  865. module_pci_driver(piix4_driver);
  866. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  867. "Philip Edelbrock <phil@netroedge.com>");
  868. MODULE_DESCRIPTION("PIIX4 SMBus driver");
  869. MODULE_LICENSE("GPL");