i2c-ismt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. * The full GNU General Public License is included in this distribution
  18. * in the file called LICENSE.GPL.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * * Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * * Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * * Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. /*
  49. * Supports the SMBus Message Transport (SMT) in the Intel Atom Processor
  50. * S12xx Product Family.
  51. *
  52. * Features supported by this driver:
  53. * Hardware PEC yes
  54. * Block buffer yes
  55. * Block process call transaction no
  56. * Slave mode no
  57. */
  58. #include <linux/module.h>
  59. #include <linux/pci.h>
  60. #include <linux/kernel.h>
  61. #include <linux/stddef.h>
  62. #include <linux/completion.h>
  63. #include <linux/dma-mapping.h>
  64. #include <linux/i2c.h>
  65. #include <linux/acpi.h>
  66. #include <linux/interrupt.h>
  67. #include <asm-generic/io-64-nonatomic-lo-hi.h>
  68. /* PCI Address Constants */
  69. #define SMBBAR 0
  70. /* PCI DIDs for the Intel SMBus Message Transport (SMT) Devices */
  71. #define PCI_DEVICE_ID_INTEL_S1200_SMT0 0x0c59
  72. #define PCI_DEVICE_ID_INTEL_S1200_SMT1 0x0c5a
  73. #define PCI_DEVICE_ID_INTEL_AVOTON_SMT 0x1f15
  74. #define ISMT_DESC_ENTRIES 2 /* number of descriptor entries */
  75. #define ISMT_MAX_RETRIES 3 /* number of SMBus retries to attempt */
  76. /* Hardware Descriptor Constants - Control Field */
  77. #define ISMT_DESC_CWRL 0x01 /* Command/Write Length */
  78. #define ISMT_DESC_BLK 0X04 /* Perform Block Transaction */
  79. #define ISMT_DESC_FAIR 0x08 /* Set fairness flag upon successful arbit. */
  80. #define ISMT_DESC_PEC 0x10 /* Packet Error Code */
  81. #define ISMT_DESC_I2C 0x20 /* I2C Enable */
  82. #define ISMT_DESC_INT 0x40 /* Interrupt */
  83. #define ISMT_DESC_SOE 0x80 /* Stop On Error */
  84. /* Hardware Descriptor Constants - Status Field */
  85. #define ISMT_DESC_SCS 0x01 /* Success */
  86. #define ISMT_DESC_DLTO 0x04 /* Data Low Time Out */
  87. #define ISMT_DESC_NAK 0x08 /* NAK Received */
  88. #define ISMT_DESC_CRC 0x10 /* CRC Error */
  89. #define ISMT_DESC_CLTO 0x20 /* Clock Low Time Out */
  90. #define ISMT_DESC_COL 0x40 /* Collisions */
  91. #define ISMT_DESC_LPR 0x80 /* Large Packet Received */
  92. /* Macros */
  93. #define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
  94. /* iSMT General Register address offsets (SMBBAR + <addr>) */
  95. #define ISMT_GR_GCTRL 0x000 /* General Control */
  96. #define ISMT_GR_SMTICL 0x008 /* SMT Interrupt Cause Location */
  97. #define ISMT_GR_ERRINTMSK 0x010 /* Error Interrupt Mask */
  98. #define ISMT_GR_ERRAERMSK 0x014 /* Error AER Mask */
  99. #define ISMT_GR_ERRSTS 0x018 /* Error Status */
  100. #define ISMT_GR_ERRINFO 0x01c /* Error Information */
  101. /* iSMT Master Registers */
  102. #define ISMT_MSTR_MDBA 0x100 /* Master Descriptor Base Address */
  103. #define ISMT_MSTR_MCTRL 0x108 /* Master Control */
  104. #define ISMT_MSTR_MSTS 0x10c /* Master Status */
  105. #define ISMT_MSTR_MDS 0x110 /* Master Descriptor Size */
  106. #define ISMT_MSTR_RPOLICY 0x114 /* Retry Policy */
  107. /* iSMT Miscellaneous Registers */
  108. #define ISMT_SPGT 0x300 /* SMBus PHY Global Timing */
  109. /* General Control Register (GCTRL) bit definitions */
  110. #define ISMT_GCTRL_TRST 0x04 /* Target Reset */
  111. #define ISMT_GCTRL_KILL 0x08 /* Kill */
  112. #define ISMT_GCTRL_SRST 0x40 /* Soft Reset */
  113. /* Master Control Register (MCTRL) bit definitions */
  114. #define ISMT_MCTRL_SS 0x01 /* Start/Stop */
  115. #define ISMT_MCTRL_MEIE 0x10 /* Master Error Interrupt Enable */
  116. #define ISMT_MCTRL_FMHP 0x00ff0000 /* Firmware Master Head Ptr (FMHP) */
  117. /* Master Status Register (MSTS) bit definitions */
  118. #define ISMT_MSTS_HMTP 0xff0000 /* HW Master Tail Pointer (HMTP) */
  119. #define ISMT_MSTS_MIS 0x20 /* Master Interrupt Status (MIS) */
  120. #define ISMT_MSTS_MEIS 0x10 /* Master Error Int Status (MEIS) */
  121. #define ISMT_MSTS_IP 0x01 /* In Progress */
  122. /* Master Descriptor Size (MDS) bit definitions */
  123. #define ISMT_MDS_MASK 0xff /* Master Descriptor Size mask (MDS) */
  124. /* SMBus PHY Global Timing Register (SPGT) bit definitions */
  125. #define ISMT_SPGT_SPD_MASK 0xc0000000 /* SMBus Speed mask */
  126. #define ISMT_SPGT_SPD_80K 0x00 /* 80 kHz */
  127. #define ISMT_SPGT_SPD_100K (0x1 << 30) /* 100 kHz */
  128. #define ISMT_SPGT_SPD_400K (0x2 << 30) /* 400 kHz */
  129. #define ISMT_SPGT_SPD_1M (0x3 << 30) /* 1 MHz */
  130. /* MSI Control Register (MSICTL) bit definitions */
  131. #define ISMT_MSICTL_MSIE 0x01 /* MSI Enable */
  132. /* iSMT Hardware Descriptor */
  133. struct ismt_desc {
  134. u8 tgtaddr_rw; /* target address & r/w bit */
  135. u8 wr_len_cmd; /* write length in bytes or a command */
  136. u8 rd_len; /* read length */
  137. u8 control; /* control bits */
  138. u8 status; /* status bits */
  139. u8 retry; /* collision retry and retry count */
  140. u8 rxbytes; /* received bytes */
  141. u8 txbytes; /* transmitted bytes */
  142. u32 dptr_low; /* lower 32 bit of the data pointer */
  143. u32 dptr_high; /* upper 32 bit of the data pointer */
  144. } __packed;
  145. struct ismt_priv {
  146. struct i2c_adapter adapter;
  147. void *smba; /* PCI BAR */
  148. struct pci_dev *pci_dev;
  149. struct ismt_desc *hw; /* descriptor virt base addr */
  150. dma_addr_t io_rng_dma; /* descriptor HW base addr */
  151. u8 head; /* ring buffer head pointer */
  152. struct completion cmp; /* interrupt completion */
  153. u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* temp R/W data buffer */
  154. bool using_msi; /* type of interrupt flag */
  155. };
  156. /**
  157. * ismt_ids - PCI device IDs supported by this driver
  158. */
  159. static const struct pci_device_id ismt_ids[] = {
  160. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
  161. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
  162. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMT) },
  163. { 0, }
  164. };
  165. MODULE_DEVICE_TABLE(pci, ismt_ids);
  166. /* Bus speed control bits for slow debuggers - refer to the docs for usage */
  167. static unsigned int bus_speed;
  168. module_param(bus_speed, uint, S_IRUGO);
  169. MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
  170. /**
  171. * __ismt_desc_dump() - dump the contents of a specific descriptor
  172. */
  173. static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
  174. {
  175. dev_dbg(dev, "Descriptor struct: %p\n", desc);
  176. dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
  177. dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
  178. dev_dbg(dev, "\trd_len= 0x%02X\n", desc->rd_len);
  179. dev_dbg(dev, "\tcontrol= 0x%02X\n", desc->control);
  180. dev_dbg(dev, "\tstatus= 0x%02X\n", desc->status);
  181. dev_dbg(dev, "\tretry= 0x%02X\n", desc->retry);
  182. dev_dbg(dev, "\trxbytes= 0x%02X\n", desc->rxbytes);
  183. dev_dbg(dev, "\ttxbytes= 0x%02X\n", desc->txbytes);
  184. dev_dbg(dev, "\tdptr_low= 0x%08X\n", desc->dptr_low);
  185. dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
  186. }
  187. /**
  188. * ismt_desc_dump() - dump the contents of a descriptor for debug purposes
  189. * @priv: iSMT private data
  190. */
  191. static void ismt_desc_dump(struct ismt_priv *priv)
  192. {
  193. struct device *dev = &priv->pci_dev->dev;
  194. struct ismt_desc *desc = &priv->hw[priv->head];
  195. dev_dbg(dev, "Dump of the descriptor struct: 0x%X\n", priv->head);
  196. __ismt_desc_dump(dev, desc);
  197. }
  198. /**
  199. * ismt_gen_reg_dump() - dump the iSMT General Registers
  200. * @priv: iSMT private data
  201. */
  202. static void ismt_gen_reg_dump(struct ismt_priv *priv)
  203. {
  204. struct device *dev = &priv->pci_dev->dev;
  205. dev_dbg(dev, "Dump of the iSMT General Registers\n");
  206. dev_dbg(dev, " GCTRL.... : (0x%p)=0x%X\n",
  207. priv->smba + ISMT_GR_GCTRL,
  208. readl(priv->smba + ISMT_GR_GCTRL));
  209. dev_dbg(dev, " SMTICL... : (0x%p)=0x%016llX\n",
  210. priv->smba + ISMT_GR_SMTICL,
  211. (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
  212. dev_dbg(dev, " ERRINTMSK : (0x%p)=0x%X\n",
  213. priv->smba + ISMT_GR_ERRINTMSK,
  214. readl(priv->smba + ISMT_GR_ERRINTMSK));
  215. dev_dbg(dev, " ERRAERMSK : (0x%p)=0x%X\n",
  216. priv->smba + ISMT_GR_ERRAERMSK,
  217. readl(priv->smba + ISMT_GR_ERRAERMSK));
  218. dev_dbg(dev, " ERRSTS... : (0x%p)=0x%X\n",
  219. priv->smba + ISMT_GR_ERRSTS,
  220. readl(priv->smba + ISMT_GR_ERRSTS));
  221. dev_dbg(dev, " ERRINFO.. : (0x%p)=0x%X\n",
  222. priv->smba + ISMT_GR_ERRINFO,
  223. readl(priv->smba + ISMT_GR_ERRINFO));
  224. }
  225. /**
  226. * ismt_mstr_reg_dump() - dump the iSMT Master Registers
  227. * @priv: iSMT private data
  228. */
  229. static void ismt_mstr_reg_dump(struct ismt_priv *priv)
  230. {
  231. struct device *dev = &priv->pci_dev->dev;
  232. dev_dbg(dev, "Dump of the iSMT Master Registers\n");
  233. dev_dbg(dev, " MDBA..... : (0x%p)=0x%016llX\n",
  234. priv->smba + ISMT_MSTR_MDBA,
  235. (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
  236. dev_dbg(dev, " MCTRL.... : (0x%p)=0x%X\n",
  237. priv->smba + ISMT_MSTR_MCTRL,
  238. readl(priv->smba + ISMT_MSTR_MCTRL));
  239. dev_dbg(dev, " MSTS..... : (0x%p)=0x%X\n",
  240. priv->smba + ISMT_MSTR_MSTS,
  241. readl(priv->smba + ISMT_MSTR_MSTS));
  242. dev_dbg(dev, " MDS...... : (0x%p)=0x%X\n",
  243. priv->smba + ISMT_MSTR_MDS,
  244. readl(priv->smba + ISMT_MSTR_MDS));
  245. dev_dbg(dev, " RPOLICY.. : (0x%p)=0x%X\n",
  246. priv->smba + ISMT_MSTR_RPOLICY,
  247. readl(priv->smba + ISMT_MSTR_RPOLICY));
  248. dev_dbg(dev, " SPGT..... : (0x%p)=0x%X\n",
  249. priv->smba + ISMT_SPGT,
  250. readl(priv->smba + ISMT_SPGT));
  251. }
  252. /**
  253. * ismt_submit_desc() - add a descriptor to the ring
  254. * @priv: iSMT private data
  255. */
  256. static void ismt_submit_desc(struct ismt_priv *priv)
  257. {
  258. uint fmhp;
  259. uint val;
  260. ismt_desc_dump(priv);
  261. ismt_gen_reg_dump(priv);
  262. ismt_mstr_reg_dump(priv);
  263. /* Set the FMHP (Firmware Master Head Pointer)*/
  264. fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
  265. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  266. writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
  267. priv->smba + ISMT_MSTR_MCTRL);
  268. /* Set the start bit */
  269. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  270. writel(val | ISMT_MCTRL_SS,
  271. priv->smba + ISMT_MSTR_MCTRL);
  272. }
  273. /**
  274. * ismt_process_desc() - handle the completion of the descriptor
  275. * @desc: the iSMT hardware descriptor
  276. * @data: data buffer from the upper layer
  277. * @priv: ismt_priv struct holding our dma buffer
  278. * @size: SMBus transaction type
  279. * @read_write: flag to indicate if this is a read or write
  280. */
  281. static int ismt_process_desc(const struct ismt_desc *desc,
  282. union i2c_smbus_data *data,
  283. struct ismt_priv *priv, int size,
  284. char read_write)
  285. {
  286. u8 *dma_buffer = priv->dma_buffer;
  287. dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
  288. __ismt_desc_dump(&priv->pci_dev->dev, desc);
  289. if (desc->status & ISMT_DESC_SCS) {
  290. if (read_write == I2C_SMBUS_WRITE &&
  291. size != I2C_SMBUS_PROC_CALL)
  292. return 0;
  293. switch (size) {
  294. case I2C_SMBUS_BYTE:
  295. case I2C_SMBUS_BYTE_DATA:
  296. data->byte = dma_buffer[0];
  297. break;
  298. case I2C_SMBUS_WORD_DATA:
  299. case I2C_SMBUS_PROC_CALL:
  300. data->word = dma_buffer[0] | (dma_buffer[1] << 8);
  301. break;
  302. case I2C_SMBUS_BLOCK_DATA:
  303. case I2C_SMBUS_I2C_BLOCK_DATA:
  304. memcpy(&data->block[1], dma_buffer, desc->rxbytes);
  305. data->block[0] = desc->rxbytes;
  306. break;
  307. }
  308. return 0;
  309. }
  310. if (likely(desc->status & ISMT_DESC_NAK))
  311. return -ENXIO;
  312. if (desc->status & ISMT_DESC_CRC)
  313. return -EBADMSG;
  314. if (desc->status & ISMT_DESC_COL)
  315. return -EAGAIN;
  316. if (desc->status & ISMT_DESC_LPR)
  317. return -EPROTO;
  318. if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
  319. return -ETIMEDOUT;
  320. return -EIO;
  321. }
  322. /**
  323. * ismt_access() - process an SMBus command
  324. * @adap: the i2c host adapter
  325. * @addr: address of the i2c/SMBus target
  326. * @flags: command options
  327. * @read_write: read from or write to device
  328. * @command: the i2c/SMBus command to issue
  329. * @size: SMBus transaction type
  330. * @data: read/write data buffer
  331. */
  332. static int ismt_access(struct i2c_adapter *adap, u16 addr,
  333. unsigned short flags, char read_write, u8 command,
  334. int size, union i2c_smbus_data *data)
  335. {
  336. int ret;
  337. dma_addr_t dma_addr = 0; /* address of the data buffer */
  338. u8 dma_size = 0;
  339. enum dma_data_direction dma_direction = 0;
  340. struct ismt_desc *desc;
  341. struct ismt_priv *priv = i2c_get_adapdata(adap);
  342. struct device *dev = &priv->pci_dev->dev;
  343. desc = &priv->hw[priv->head];
  344. /* Initialize the DMA buffer */
  345. memset(priv->dma_buffer, 0, sizeof(priv->dma_buffer));
  346. /* Initialize the descriptor */
  347. memset(desc, 0, sizeof(struct ismt_desc));
  348. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
  349. /* Initialize common control bits */
  350. if (likely(priv->using_msi))
  351. desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
  352. else
  353. desc->control = ISMT_DESC_FAIR;
  354. if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
  355. && (size != I2C_SMBUS_I2C_BLOCK_DATA))
  356. desc->control |= ISMT_DESC_PEC;
  357. switch (size) {
  358. case I2C_SMBUS_QUICK:
  359. dev_dbg(dev, "I2C_SMBUS_QUICK\n");
  360. break;
  361. case I2C_SMBUS_BYTE:
  362. if (read_write == I2C_SMBUS_WRITE) {
  363. /*
  364. * Send Byte
  365. * The command field contains the write data
  366. */
  367. dev_dbg(dev, "I2C_SMBUS_BYTE: WRITE\n");
  368. desc->control |= ISMT_DESC_CWRL;
  369. desc->wr_len_cmd = command;
  370. } else {
  371. /* Receive Byte */
  372. dev_dbg(dev, "I2C_SMBUS_BYTE: READ\n");
  373. dma_size = 1;
  374. dma_direction = DMA_FROM_DEVICE;
  375. desc->rd_len = 1;
  376. }
  377. break;
  378. case I2C_SMBUS_BYTE_DATA:
  379. if (read_write == I2C_SMBUS_WRITE) {
  380. /*
  381. * Write Byte
  382. * Command plus 1 data byte
  383. */
  384. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: WRITE\n");
  385. desc->wr_len_cmd = 2;
  386. dma_size = 2;
  387. dma_direction = DMA_TO_DEVICE;
  388. priv->dma_buffer[0] = command;
  389. priv->dma_buffer[1] = data->byte;
  390. } else {
  391. /* Read Byte */
  392. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: READ\n");
  393. desc->control |= ISMT_DESC_CWRL;
  394. desc->wr_len_cmd = command;
  395. desc->rd_len = 1;
  396. dma_size = 1;
  397. dma_direction = DMA_FROM_DEVICE;
  398. }
  399. break;
  400. case I2C_SMBUS_WORD_DATA:
  401. if (read_write == I2C_SMBUS_WRITE) {
  402. /* Write Word */
  403. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: WRITE\n");
  404. desc->wr_len_cmd = 3;
  405. dma_size = 3;
  406. dma_direction = DMA_TO_DEVICE;
  407. priv->dma_buffer[0] = command;
  408. priv->dma_buffer[1] = data->word & 0xff;
  409. priv->dma_buffer[2] = data->word >> 8;
  410. } else {
  411. /* Read Word */
  412. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: READ\n");
  413. desc->wr_len_cmd = command;
  414. desc->control |= ISMT_DESC_CWRL;
  415. desc->rd_len = 2;
  416. dma_size = 2;
  417. dma_direction = DMA_FROM_DEVICE;
  418. }
  419. break;
  420. case I2C_SMBUS_PROC_CALL:
  421. dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
  422. desc->wr_len_cmd = 3;
  423. desc->rd_len = 2;
  424. dma_size = 3;
  425. dma_direction = DMA_BIDIRECTIONAL;
  426. priv->dma_buffer[0] = command;
  427. priv->dma_buffer[1] = data->word & 0xff;
  428. priv->dma_buffer[2] = data->word >> 8;
  429. break;
  430. case I2C_SMBUS_BLOCK_DATA:
  431. if (read_write == I2C_SMBUS_WRITE) {
  432. /* Block Write */
  433. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: WRITE\n");
  434. dma_size = data->block[0] + 1;
  435. dma_direction = DMA_TO_DEVICE;
  436. desc->wr_len_cmd = dma_size;
  437. desc->control |= ISMT_DESC_BLK;
  438. priv->dma_buffer[0] = command;
  439. memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
  440. } else {
  441. /* Block Read */
  442. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
  443. dma_size = I2C_SMBUS_BLOCK_MAX;
  444. dma_direction = DMA_FROM_DEVICE;
  445. desc->rd_len = dma_size;
  446. desc->wr_len_cmd = command;
  447. desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
  448. }
  449. break;
  450. case I2C_SMBUS_I2C_BLOCK_DATA:
  451. /* Make sure the length is valid */
  452. if (data->block[0] < 1)
  453. data->block[0] = 1;
  454. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  455. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  456. if (read_write == I2C_SMBUS_WRITE) {
  457. /* i2c Block Write */
  458. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: WRITE\n");
  459. dma_size = data->block[0] + 1;
  460. dma_direction = DMA_TO_DEVICE;
  461. desc->wr_len_cmd = dma_size;
  462. desc->control |= ISMT_DESC_I2C;
  463. priv->dma_buffer[0] = command;
  464. memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
  465. } else {
  466. /* i2c Block Read */
  467. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n");
  468. dma_size = data->block[0];
  469. dma_direction = DMA_FROM_DEVICE;
  470. desc->rd_len = dma_size;
  471. desc->wr_len_cmd = command;
  472. desc->control |= (ISMT_DESC_I2C | ISMT_DESC_CWRL);
  473. /*
  474. * Per the "Table 15-15. I2C Commands",
  475. * in the External Design Specification (EDS),
  476. * (Document Number: 508084, Revision: 2.0),
  477. * the _rw bit must be 0
  478. */
  479. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 0);
  480. }
  481. break;
  482. default:
  483. dev_err(dev, "Unsupported transaction %d\n",
  484. size);
  485. return -EOPNOTSUPP;
  486. }
  487. /* map the data buffer */
  488. if (dma_size != 0) {
  489. dev_dbg(dev, " dev=%p\n", dev);
  490. dev_dbg(dev, " data=%p\n", data);
  491. dev_dbg(dev, " dma_buffer=%p\n", priv->dma_buffer);
  492. dev_dbg(dev, " dma_size=%d\n", dma_size);
  493. dev_dbg(dev, " dma_direction=%d\n", dma_direction);
  494. dma_addr = dma_map_single(dev,
  495. priv->dma_buffer,
  496. dma_size,
  497. dma_direction);
  498. if (dma_mapping_error(dev, dma_addr)) {
  499. dev_err(dev, "Error in mapping dma buffer %p\n",
  500. priv->dma_buffer);
  501. return -EIO;
  502. }
  503. dev_dbg(dev, " dma_addr = 0x%016llX\n",
  504. (unsigned long long)dma_addr);
  505. desc->dptr_low = lower_32_bits(dma_addr);
  506. desc->dptr_high = upper_32_bits(dma_addr);
  507. }
  508. reinit_completion(&priv->cmp);
  509. /* Add the descriptor */
  510. ismt_submit_desc(priv);
  511. /* Now we wait for interrupt completion, 1s */
  512. ret = wait_for_completion_timeout(&priv->cmp, HZ*1);
  513. /* unmap the data buffer */
  514. if (dma_size != 0)
  515. dma_unmap_single(&adap->dev, dma_addr, dma_size, dma_direction);
  516. if (unlikely(!ret)) {
  517. dev_err(dev, "completion wait timed out\n");
  518. ret = -ETIMEDOUT;
  519. goto out;
  520. }
  521. /* do any post processing of the descriptor here */
  522. ret = ismt_process_desc(desc, data, priv, size, read_write);
  523. out:
  524. /* Update the ring pointer */
  525. priv->head++;
  526. priv->head %= ISMT_DESC_ENTRIES;
  527. return ret;
  528. }
  529. /**
  530. * ismt_func() - report which i2c commands are supported by this adapter
  531. * @adap: the i2c host adapter
  532. */
  533. static u32 ismt_func(struct i2c_adapter *adap)
  534. {
  535. return I2C_FUNC_SMBUS_QUICK |
  536. I2C_FUNC_SMBUS_BYTE |
  537. I2C_FUNC_SMBUS_BYTE_DATA |
  538. I2C_FUNC_SMBUS_WORD_DATA |
  539. I2C_FUNC_SMBUS_PROC_CALL |
  540. I2C_FUNC_SMBUS_BLOCK_DATA |
  541. I2C_FUNC_SMBUS_I2C_BLOCK |
  542. I2C_FUNC_SMBUS_PEC;
  543. }
  544. /**
  545. * smbus_algorithm - the adapter algorithm and supported functionality
  546. * @smbus_xfer: the adapter algorithm
  547. * @functionality: functionality supported by the adapter
  548. */
  549. static const struct i2c_algorithm smbus_algorithm = {
  550. .smbus_xfer = ismt_access,
  551. .functionality = ismt_func,
  552. };
  553. /**
  554. * ismt_handle_isr() - interrupt handler bottom half
  555. * @priv: iSMT private data
  556. */
  557. static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
  558. {
  559. complete(&priv->cmp);
  560. return IRQ_HANDLED;
  561. }
  562. /**
  563. * ismt_do_interrupt() - IRQ interrupt handler
  564. * @vec: interrupt vector
  565. * @data: iSMT private data
  566. */
  567. static irqreturn_t ismt_do_interrupt(int vec, void *data)
  568. {
  569. u32 val;
  570. struct ismt_priv *priv = data;
  571. /*
  572. * check to see it's our interrupt, return IRQ_NONE if not ours
  573. * since we are sharing interrupt
  574. */
  575. val = readl(priv->smba + ISMT_MSTR_MSTS);
  576. if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
  577. return IRQ_NONE;
  578. else
  579. writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
  580. priv->smba + ISMT_MSTR_MSTS);
  581. return ismt_handle_isr(priv);
  582. }
  583. /**
  584. * ismt_do_msi_interrupt() - MSI interrupt handler
  585. * @vec: interrupt vector
  586. * @data: iSMT private data
  587. */
  588. static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
  589. {
  590. return ismt_handle_isr(data);
  591. }
  592. /**
  593. * ismt_hw_init() - initialize the iSMT hardware
  594. * @priv: iSMT private data
  595. */
  596. static void ismt_hw_init(struct ismt_priv *priv)
  597. {
  598. u32 val;
  599. struct device *dev = &priv->pci_dev->dev;
  600. /* initialize the Master Descriptor Base Address (MDBA) */
  601. writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
  602. /* initialize the Master Control Register (MCTRL) */
  603. writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
  604. /* initialize the Master Status Register (MSTS) */
  605. writel(0, priv->smba + ISMT_MSTR_MSTS);
  606. /* initialize the Master Descriptor Size (MDS) */
  607. val = readl(priv->smba + ISMT_MSTR_MDS);
  608. writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
  609. priv->smba + ISMT_MSTR_MDS);
  610. /*
  611. * Set the SMBus speed (could use this for slow HW debuggers)
  612. */
  613. val = readl(priv->smba + ISMT_SPGT);
  614. switch (bus_speed) {
  615. case 0:
  616. break;
  617. case 80:
  618. dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
  619. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
  620. priv->smba + ISMT_SPGT);
  621. break;
  622. case 100:
  623. dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
  624. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
  625. priv->smba + ISMT_SPGT);
  626. break;
  627. case 400:
  628. dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
  629. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
  630. priv->smba + ISMT_SPGT);
  631. break;
  632. case 1000:
  633. dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
  634. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
  635. priv->smba + ISMT_SPGT);
  636. break;
  637. default:
  638. dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
  639. break;
  640. }
  641. val = readl(priv->smba + ISMT_SPGT);
  642. switch (val & ISMT_SPGT_SPD_MASK) {
  643. case ISMT_SPGT_SPD_80K:
  644. bus_speed = 80;
  645. break;
  646. case ISMT_SPGT_SPD_100K:
  647. bus_speed = 100;
  648. break;
  649. case ISMT_SPGT_SPD_400K:
  650. bus_speed = 400;
  651. break;
  652. case ISMT_SPGT_SPD_1M:
  653. bus_speed = 1000;
  654. break;
  655. }
  656. dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
  657. }
  658. /**
  659. * ismt_dev_init() - initialize the iSMT data structures
  660. * @priv: iSMT private data
  661. */
  662. static int ismt_dev_init(struct ismt_priv *priv)
  663. {
  664. /* allocate memory for the descriptor */
  665. priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
  666. (ISMT_DESC_ENTRIES
  667. * sizeof(struct ismt_desc)),
  668. &priv->io_rng_dma,
  669. GFP_KERNEL);
  670. if (!priv->hw)
  671. return -ENOMEM;
  672. memset(priv->hw, 0, (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc)));
  673. priv->head = 0;
  674. init_completion(&priv->cmp);
  675. return 0;
  676. }
  677. /**
  678. * ismt_int_init() - initialize interrupts
  679. * @priv: iSMT private data
  680. */
  681. static int ismt_int_init(struct ismt_priv *priv)
  682. {
  683. int err;
  684. /* Try using MSI interrupts */
  685. err = pci_enable_msi(priv->pci_dev);
  686. if (err) {
  687. dev_warn(&priv->pci_dev->dev,
  688. "Unable to use MSI interrupts, falling back to legacy\n");
  689. goto intx;
  690. }
  691. err = devm_request_irq(&priv->pci_dev->dev,
  692. priv->pci_dev->irq,
  693. ismt_do_msi_interrupt,
  694. 0,
  695. "ismt-msi",
  696. priv);
  697. if (err) {
  698. pci_disable_msi(priv->pci_dev);
  699. goto intx;
  700. }
  701. priv->using_msi = true;
  702. goto done;
  703. /* Try using legacy interrupts */
  704. intx:
  705. err = devm_request_irq(&priv->pci_dev->dev,
  706. priv->pci_dev->irq,
  707. ismt_do_interrupt,
  708. IRQF_SHARED,
  709. "ismt-intx",
  710. priv);
  711. if (err) {
  712. dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
  713. return -ENODEV;
  714. }
  715. priv->using_msi = false;
  716. done:
  717. return 0;
  718. }
  719. static struct pci_driver ismt_driver;
  720. /**
  721. * ismt_probe() - probe for iSMT devices
  722. * @pdev: PCI-Express device
  723. * @id: PCI-Express device ID
  724. */
  725. static int
  726. ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  727. {
  728. int err;
  729. struct ismt_priv *priv;
  730. unsigned long start, len;
  731. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  732. if (!priv)
  733. return -ENOMEM;
  734. pci_set_drvdata(pdev, priv);
  735. i2c_set_adapdata(&priv->adapter, priv);
  736. priv->adapter.owner = THIS_MODULE;
  737. priv->adapter.class = I2C_CLASS_HWMON;
  738. priv->adapter.algo = &smbus_algorithm;
  739. /* set up the sysfs linkage to our parent device */
  740. priv->adapter.dev.parent = &pdev->dev;
  741. /* number of retries on lost arbitration */
  742. priv->adapter.retries = ISMT_MAX_RETRIES;
  743. priv->pci_dev = pdev;
  744. err = pcim_enable_device(pdev);
  745. if (err) {
  746. dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
  747. err);
  748. return err;
  749. }
  750. /* enable bus mastering */
  751. pci_set_master(pdev);
  752. /* Determine the address of the SMBus area */
  753. start = pci_resource_start(pdev, SMBBAR);
  754. len = pci_resource_len(pdev, SMBBAR);
  755. if (!start || !len) {
  756. dev_err(&pdev->dev,
  757. "SMBus base address uninitialized, upgrade BIOS\n");
  758. return -ENODEV;
  759. }
  760. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  761. "SMBus iSMT adapter at %lx", start);
  762. dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
  763. dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
  764. err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
  765. if (err) {
  766. dev_err(&pdev->dev, "ACPI resource conflict!\n");
  767. return err;
  768. }
  769. err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
  770. if (err) {
  771. dev_err(&pdev->dev,
  772. "Failed to request SMBus region 0x%lx-0x%lx\n",
  773. start, start + len);
  774. return err;
  775. }
  776. priv->smba = pcim_iomap(pdev, SMBBAR, len);
  777. if (!priv->smba) {
  778. dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
  779. err = -ENODEV;
  780. goto fail;
  781. }
  782. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
  783. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
  784. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  785. (pci_set_consistent_dma_mask(pdev,
  786. DMA_BIT_MASK(32)) != 0)) {
  787. dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
  788. pdev);
  789. err = -ENODEV;
  790. goto fail;
  791. }
  792. }
  793. err = ismt_dev_init(priv);
  794. if (err)
  795. goto fail;
  796. ismt_hw_init(priv);
  797. err = ismt_int_init(priv);
  798. if (err)
  799. goto fail;
  800. err = i2c_add_adapter(&priv->adapter);
  801. if (err) {
  802. dev_err(&pdev->dev, "Failed to add SMBus iSMT adapter\n");
  803. err = -ENODEV;
  804. goto fail;
  805. }
  806. return 0;
  807. fail:
  808. pci_release_region(pdev, SMBBAR);
  809. return err;
  810. }
  811. /**
  812. * ismt_remove() - release driver resources
  813. * @pdev: PCI-Express device
  814. */
  815. static void ismt_remove(struct pci_dev *pdev)
  816. {
  817. struct ismt_priv *priv = pci_get_drvdata(pdev);
  818. i2c_del_adapter(&priv->adapter);
  819. pci_release_region(pdev, SMBBAR);
  820. }
  821. /**
  822. * ismt_suspend() - place the device in suspend
  823. * @pdev: PCI-Express device
  824. * @mesg: PM message
  825. */
  826. #ifdef CONFIG_PM
  827. static int ismt_suspend(struct pci_dev *pdev, pm_message_t mesg)
  828. {
  829. pci_save_state(pdev);
  830. pci_set_power_state(pdev, pci_choose_state(pdev, mesg));
  831. return 0;
  832. }
  833. /**
  834. * ismt_resume() - PCI resume code
  835. * @pdev: PCI-Express device
  836. */
  837. static int ismt_resume(struct pci_dev *pdev)
  838. {
  839. pci_set_power_state(pdev, PCI_D0);
  840. pci_restore_state(pdev);
  841. return pci_enable_device(pdev);
  842. }
  843. #else
  844. #define ismt_suspend NULL
  845. #define ismt_resume NULL
  846. #endif
  847. static struct pci_driver ismt_driver = {
  848. .name = "ismt_smbus",
  849. .id_table = ismt_ids,
  850. .probe = ismt_probe,
  851. .remove = ismt_remove,
  852. .suspend = ismt_suspend,
  853. .resume = ismt_resume,
  854. };
  855. module_pci_driver(ismt_driver);
  856. MODULE_LICENSE("Dual BSD/GPL");
  857. MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
  858. MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");