i2c-piix4.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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, 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. switch (PIIX4_dev->device) {
  314. case PCI_DEVICE_ID_AMD_KERNCZ_SMBUS:
  315. piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
  316. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
  317. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
  318. break;
  319. case PCI_DEVICE_ID_AMD_HUDSON2_SMBUS:
  320. default:
  321. piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
  322. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
  323. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
  324. break;
  325. }
  326. } else {
  327. if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2,
  328. "sb800_piix4_smb")) {
  329. release_region(piix4_smba, SMBIOSIZE);
  330. return -EBUSY;
  331. }
  332. outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
  333. port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
  334. piix4_port_sel_sb800 = (port_sel & 0x01) ?
  335. SB800_PIIX4_PORT_IDX_ALT :
  336. SB800_PIIX4_PORT_IDX;
  337. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
  338. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
  339. release_region(SB800_PIIX4_SMB_IDX, 2);
  340. }
  341. dev_info(&PIIX4_dev->dev,
  342. "Using register 0x%02x for SMBus port selection\n",
  343. (unsigned int)piix4_port_sel_sb800);
  344. return piix4_smba;
  345. }
  346. static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
  347. const struct pci_device_id *id,
  348. unsigned short base_reg_addr)
  349. {
  350. /* Set up auxiliary SMBus controllers found on some
  351. * AMD chipsets e.g. SP5100 (SB700 derivative) */
  352. unsigned short piix4_smba;
  353. /* Read address of auxiliary SMBus controller */
  354. pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
  355. if ((piix4_smba & 1) == 0) {
  356. dev_dbg(&PIIX4_dev->dev,
  357. "Auxiliary SMBus controller not enabled\n");
  358. return -ENODEV;
  359. }
  360. piix4_smba &= 0xfff0;
  361. if (piix4_smba == 0) {
  362. dev_dbg(&PIIX4_dev->dev,
  363. "Auxiliary SMBus base address uninitialized\n");
  364. return -ENODEV;
  365. }
  366. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  367. return -ENODEV;
  368. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  369. dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
  370. "already in use!\n", piix4_smba);
  371. return -EBUSY;
  372. }
  373. dev_info(&PIIX4_dev->dev,
  374. "Auxiliary SMBus Host Controller at 0x%x\n",
  375. piix4_smba);
  376. return piix4_smba;
  377. }
  378. static int piix4_transaction(struct i2c_adapter *piix4_adapter)
  379. {
  380. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
  381. unsigned short piix4_smba = adapdata->smba;
  382. int temp;
  383. int result = 0;
  384. int timeout = 0;
  385. dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
  386. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  387. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  388. inb_p(SMBHSTDAT1));
  389. /* Make sure the SMBus host is ready to start transmitting */
  390. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  391. dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
  392. "Resetting...\n", temp);
  393. outb_p(temp, SMBHSTSTS);
  394. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  395. dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
  396. return -EBUSY;
  397. } else {
  398. dev_dbg(&piix4_adapter->dev, "Successful!\n");
  399. }
  400. }
  401. /* start the transaction by setting bit 6 */
  402. outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
  403. /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
  404. if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
  405. usleep_range(2000, 2100);
  406. else
  407. usleep_range(250, 500);
  408. while ((++timeout < MAX_TIMEOUT) &&
  409. ((temp = inb_p(SMBHSTSTS)) & 0x01))
  410. usleep_range(250, 500);
  411. /* If the SMBus is still busy, we give up */
  412. if (timeout == MAX_TIMEOUT) {
  413. dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
  414. result = -ETIMEDOUT;
  415. }
  416. if (temp & 0x10) {
  417. result = -EIO;
  418. dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
  419. }
  420. if (temp & 0x08) {
  421. result = -EIO;
  422. dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
  423. "locked until next hard reset. (sorry!)\n");
  424. /* Clock stops and slave is stuck in mid-transmission */
  425. }
  426. if (temp & 0x04) {
  427. result = -ENXIO;
  428. dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
  429. }
  430. if (inb_p(SMBHSTSTS) != 0x00)
  431. outb_p(inb(SMBHSTSTS), SMBHSTSTS);
  432. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  433. dev_err(&piix4_adapter->dev, "Failed reset at end of "
  434. "transaction (%02x)\n", temp);
  435. }
  436. dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
  437. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  438. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  439. inb_p(SMBHSTDAT1));
  440. return result;
  441. }
  442. /* Return negative errno on error. */
  443. static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
  444. unsigned short flags, char read_write,
  445. u8 command, int size, union i2c_smbus_data * data)
  446. {
  447. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  448. unsigned short piix4_smba = adapdata->smba;
  449. int i, len;
  450. int status;
  451. switch (size) {
  452. case I2C_SMBUS_QUICK:
  453. outb_p((addr << 1) | read_write,
  454. SMBHSTADD);
  455. size = PIIX4_QUICK;
  456. break;
  457. case I2C_SMBUS_BYTE:
  458. outb_p((addr << 1) | read_write,
  459. SMBHSTADD);
  460. if (read_write == I2C_SMBUS_WRITE)
  461. outb_p(command, SMBHSTCMD);
  462. size = PIIX4_BYTE;
  463. break;
  464. case I2C_SMBUS_BYTE_DATA:
  465. outb_p((addr << 1) | read_write,
  466. SMBHSTADD);
  467. outb_p(command, SMBHSTCMD);
  468. if (read_write == I2C_SMBUS_WRITE)
  469. outb_p(data->byte, SMBHSTDAT0);
  470. size = PIIX4_BYTE_DATA;
  471. break;
  472. case I2C_SMBUS_WORD_DATA:
  473. outb_p((addr << 1) | read_write,
  474. SMBHSTADD);
  475. outb_p(command, SMBHSTCMD);
  476. if (read_write == I2C_SMBUS_WRITE) {
  477. outb_p(data->word & 0xff, SMBHSTDAT0);
  478. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  479. }
  480. size = PIIX4_WORD_DATA;
  481. break;
  482. case I2C_SMBUS_BLOCK_DATA:
  483. outb_p((addr << 1) | read_write,
  484. SMBHSTADD);
  485. outb_p(command, SMBHSTCMD);
  486. if (read_write == I2C_SMBUS_WRITE) {
  487. len = data->block[0];
  488. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  489. return -EINVAL;
  490. outb_p(len, SMBHSTDAT0);
  491. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  492. for (i = 1; i <= len; i++)
  493. outb_p(data->block[i], SMBBLKDAT);
  494. }
  495. size = PIIX4_BLOCK_DATA;
  496. break;
  497. default:
  498. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  499. return -EOPNOTSUPP;
  500. }
  501. outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
  502. status = piix4_transaction(adap);
  503. if (status)
  504. return status;
  505. if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
  506. return 0;
  507. switch (size) {
  508. case PIIX4_BYTE:
  509. case PIIX4_BYTE_DATA:
  510. data->byte = inb_p(SMBHSTDAT0);
  511. break;
  512. case PIIX4_WORD_DATA:
  513. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  514. break;
  515. case PIIX4_BLOCK_DATA:
  516. data->block[0] = inb_p(SMBHSTDAT0);
  517. if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
  518. return -EPROTO;
  519. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  520. for (i = 1; i <= data->block[0]; i++)
  521. data->block[i] = inb_p(SMBBLKDAT);
  522. break;
  523. }
  524. return 0;
  525. }
  526. static uint8_t piix4_imc_read(uint8_t idx)
  527. {
  528. outb_p(idx, KERNCZ_IMC_IDX);
  529. return inb_p(KERNCZ_IMC_DATA);
  530. }
  531. static void piix4_imc_write(uint8_t idx, uint8_t value)
  532. {
  533. outb_p(idx, KERNCZ_IMC_IDX);
  534. outb_p(value, KERNCZ_IMC_DATA);
  535. }
  536. static int piix4_imc_sleep(void)
  537. {
  538. int timeout = MAX_TIMEOUT;
  539. if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
  540. return -EBUSY;
  541. /* clear response register */
  542. piix4_imc_write(0x82, 0x00);
  543. /* request ownership flag */
  544. piix4_imc_write(0x83, 0xB4);
  545. /* kick off IMC Mailbox command 96 */
  546. piix4_imc_write(0x80, 0x96);
  547. while (timeout--) {
  548. if (piix4_imc_read(0x82) == 0xfa) {
  549. release_region(KERNCZ_IMC_IDX, 2);
  550. return 0;
  551. }
  552. usleep_range(1000, 2000);
  553. }
  554. release_region(KERNCZ_IMC_IDX, 2);
  555. return -ETIMEDOUT;
  556. }
  557. static void piix4_imc_wakeup(void)
  558. {
  559. int timeout = MAX_TIMEOUT;
  560. if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
  561. return;
  562. /* clear response register */
  563. piix4_imc_write(0x82, 0x00);
  564. /* release ownership flag */
  565. piix4_imc_write(0x83, 0xB5);
  566. /* kick off IMC Mailbox command 96 */
  567. piix4_imc_write(0x80, 0x96);
  568. while (timeout--) {
  569. if (piix4_imc_read(0x82) == 0xfa)
  570. break;
  571. usleep_range(1000, 2000);
  572. }
  573. release_region(KERNCZ_IMC_IDX, 2);
  574. }
  575. /*
  576. * Handles access to multiple SMBus ports on the SB800.
  577. * The port is selected by bits 2:1 of the smb_en register (0x2c).
  578. * Returns negative errno on error.
  579. *
  580. * Note: The selected port must be returned to the initial selection to avoid
  581. * problems on certain systems.
  582. */
  583. static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
  584. unsigned short flags, char read_write,
  585. u8 command, int size, union i2c_smbus_data *data)
  586. {
  587. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  588. unsigned short piix4_smba = adapdata->smba;
  589. int retries = MAX_TIMEOUT;
  590. int smbslvcnt;
  591. u8 smba_en_lo;
  592. u8 port;
  593. int retval;
  594. if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb"))
  595. return -EBUSY;
  596. /* Request the SMBUS semaphore, avoid conflicts with the IMC */
  597. smbslvcnt = inb_p(SMBSLVCNT);
  598. do {
  599. outb_p(smbslvcnt | 0x10, SMBSLVCNT);
  600. /* Check the semaphore status */
  601. smbslvcnt = inb_p(SMBSLVCNT);
  602. if (smbslvcnt & 0x10)
  603. break;
  604. usleep_range(1000, 2000);
  605. } while (--retries);
  606. /* SMBus is still owned by the IMC, we give up */
  607. if (!retries) {
  608. retval = -EBUSY;
  609. goto release;
  610. }
  611. /*
  612. * Notify the IMC (Integrated Micro Controller) if required.
  613. * Among other responsibilities, the IMC is in charge of monitoring
  614. * the System fans and temperature sensors, and act accordingly.
  615. * All this is done through SMBus and can/will collide
  616. * with our transactions if they are long (BLOCK_DATA).
  617. * Therefore we need to request the ownership flag during those
  618. * transactions.
  619. */
  620. if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc) {
  621. int ret;
  622. ret = piix4_imc_sleep();
  623. switch (ret) {
  624. case -EBUSY:
  625. dev_warn(&adap->dev,
  626. "IMC base address index region 0x%x already in use.\n",
  627. KERNCZ_IMC_IDX);
  628. break;
  629. case -ETIMEDOUT:
  630. dev_warn(&adap->dev,
  631. "Failed to communicate with the IMC.\n");
  632. break;
  633. default:
  634. break;
  635. }
  636. /* If IMC communication fails do not retry */
  637. if (ret) {
  638. dev_warn(&adap->dev,
  639. "Continuing without IMC notification.\n");
  640. adapdata->notify_imc = false;
  641. }
  642. }
  643. outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
  644. smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
  645. port = adapdata->port;
  646. if ((smba_en_lo & piix4_port_mask_sb800) != port)
  647. outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
  648. SB800_PIIX4_SMB_IDX + 1);
  649. retval = piix4_access(adap, addr, flags, read_write,
  650. command, size, data);
  651. outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
  652. /* Release the semaphore */
  653. outb_p(smbslvcnt | 0x20, SMBSLVCNT);
  654. if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc)
  655. piix4_imc_wakeup();
  656. release:
  657. release_region(SB800_PIIX4_SMB_IDX, 2);
  658. return retval;
  659. }
  660. static u32 piix4_func(struct i2c_adapter *adapter)
  661. {
  662. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  663. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  664. I2C_FUNC_SMBUS_BLOCK_DATA;
  665. }
  666. static const struct i2c_algorithm smbus_algorithm = {
  667. .smbus_xfer = piix4_access,
  668. .functionality = piix4_func,
  669. };
  670. static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
  671. .smbus_xfer = piix4_access_sb800,
  672. .functionality = piix4_func,
  673. };
  674. static const struct pci_device_id piix4_ids[] = {
  675. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
  676. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
  677. { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
  678. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
  679. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
  680. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
  681. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
  682. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
  683. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
  684. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  685. PCI_DEVICE_ID_SERVERWORKS_OSB4) },
  686. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  687. PCI_DEVICE_ID_SERVERWORKS_CSB5) },
  688. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  689. PCI_DEVICE_ID_SERVERWORKS_CSB6) },
  690. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  691. PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
  692. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  693. PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
  694. { 0, }
  695. };
  696. MODULE_DEVICE_TABLE (pci, piix4_ids);
  697. static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
  698. static struct i2c_adapter *piix4_aux_adapter;
  699. static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
  700. bool sb800_main, u8 port, bool notify_imc,
  701. const char *name, struct i2c_adapter **padap)
  702. {
  703. struct i2c_adapter *adap;
  704. struct i2c_piix4_adapdata *adapdata;
  705. int retval;
  706. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  707. if (adap == NULL) {
  708. release_region(smba, SMBIOSIZE);
  709. return -ENOMEM;
  710. }
  711. adap->owner = THIS_MODULE;
  712. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  713. adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
  714. : &smbus_algorithm;
  715. adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
  716. if (adapdata == NULL) {
  717. kfree(adap);
  718. release_region(smba, SMBIOSIZE);
  719. return -ENOMEM;
  720. }
  721. adapdata->smba = smba;
  722. adapdata->sb800_main = sb800_main;
  723. adapdata->port = port << piix4_port_shift_sb800;
  724. adapdata->notify_imc = notify_imc;
  725. /* set up the sysfs linkage to our parent device */
  726. adap->dev.parent = &dev->dev;
  727. snprintf(adap->name, sizeof(adap->name),
  728. "SMBus PIIX4 adapter%s at %04x", name, smba);
  729. i2c_set_adapdata(adap, adapdata);
  730. retval = i2c_add_adapter(adap);
  731. if (retval) {
  732. kfree(adapdata);
  733. kfree(adap);
  734. release_region(smba, SMBIOSIZE);
  735. return retval;
  736. }
  737. *padap = adap;
  738. return 0;
  739. }
  740. static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba,
  741. bool notify_imc)
  742. {
  743. struct i2c_piix4_adapdata *adapdata;
  744. int port;
  745. int retval;
  746. for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
  747. retval = piix4_add_adapter(dev, smba, true, port, notify_imc,
  748. piix4_main_port_names_sb800[port],
  749. &piix4_main_adapters[port]);
  750. if (retval < 0)
  751. goto error;
  752. }
  753. return retval;
  754. error:
  755. dev_err(&dev->dev,
  756. "Error setting up SB800 adapters. Unregistering!\n");
  757. while (--port >= 0) {
  758. adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
  759. if (adapdata->smba) {
  760. i2c_del_adapter(piix4_main_adapters[port]);
  761. kfree(adapdata);
  762. kfree(piix4_main_adapters[port]);
  763. piix4_main_adapters[port] = NULL;
  764. }
  765. }
  766. return retval;
  767. }
  768. static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
  769. {
  770. int retval;
  771. bool is_sb800 = false;
  772. if ((dev->vendor == PCI_VENDOR_ID_ATI &&
  773. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
  774. dev->revision >= 0x40) ||
  775. dev->vendor == PCI_VENDOR_ID_AMD) {
  776. bool notify_imc = false;
  777. is_sb800 = true;
  778. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  779. dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) {
  780. u8 imc;
  781. /*
  782. * Detect if IMC is active or not, this method is
  783. * described on coreboot's AMD IMC notes
  784. */
  785. pci_bus_read_config_byte(dev->bus, PCI_DEVFN(0x14, 3),
  786. 0x40, &imc);
  787. if (imc & 0x80)
  788. notify_imc = true;
  789. }
  790. /* base address location etc changed in SB800 */
  791. retval = piix4_setup_sb800(dev, id, 0);
  792. if (retval < 0)
  793. return retval;
  794. /*
  795. * Try to register multiplexed main SMBus adapter,
  796. * give up if we can't
  797. */
  798. retval = piix4_add_adapters_sb800(dev, retval, notify_imc);
  799. if (retval < 0)
  800. return retval;
  801. } else {
  802. retval = piix4_setup(dev, id);
  803. if (retval < 0)
  804. return retval;
  805. /* Try to register main SMBus adapter, give up if we can't */
  806. retval = piix4_add_adapter(dev, retval, false, 0, false, "",
  807. &piix4_main_adapters[0]);
  808. if (retval < 0)
  809. return retval;
  810. }
  811. /* Check for auxiliary SMBus on some AMD chipsets */
  812. retval = -ENODEV;
  813. if (dev->vendor == PCI_VENDOR_ID_ATI &&
  814. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
  815. if (dev->revision < 0x40) {
  816. retval = piix4_setup_aux(dev, id, 0x58);
  817. } else {
  818. /* SB800 added aux bus too */
  819. retval = piix4_setup_sb800(dev, id, 1);
  820. }
  821. }
  822. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  823. dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
  824. retval = piix4_setup_sb800(dev, id, 1);
  825. }
  826. if (retval > 0) {
  827. /* Try to add the aux adapter if it exists,
  828. * piix4_add_adapter will clean up if this fails */
  829. piix4_add_adapter(dev, retval, false, 0, false,
  830. is_sb800 ? piix4_aux_port_name_sb800 : "",
  831. &piix4_aux_adapter);
  832. }
  833. return 0;
  834. }
  835. static void piix4_adap_remove(struct i2c_adapter *adap)
  836. {
  837. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  838. if (adapdata->smba) {
  839. i2c_del_adapter(adap);
  840. if (adapdata->port == (0 << piix4_port_shift_sb800))
  841. release_region(adapdata->smba, SMBIOSIZE);
  842. kfree(adapdata);
  843. kfree(adap);
  844. }
  845. }
  846. static void piix4_remove(struct pci_dev *dev)
  847. {
  848. int port = PIIX4_MAX_ADAPTERS;
  849. while (--port >= 0) {
  850. if (piix4_main_adapters[port]) {
  851. piix4_adap_remove(piix4_main_adapters[port]);
  852. piix4_main_adapters[port] = NULL;
  853. }
  854. }
  855. if (piix4_aux_adapter) {
  856. piix4_adap_remove(piix4_aux_adapter);
  857. piix4_aux_adapter = NULL;
  858. }
  859. }
  860. static struct pci_driver piix4_driver = {
  861. .name = "piix4_smbus",
  862. .id_table = piix4_ids,
  863. .probe = piix4_probe,
  864. .remove = piix4_remove,
  865. };
  866. module_pci_driver(piix4_driver);
  867. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  868. "Philip Edelbrock <phil@netroedge.com>");
  869. MODULE_DESCRIPTION("PIIX4 SMBus driver");
  870. MODULE_LICENSE("GPL");