i2c-ismt.c 27 KB

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