i2c-ismt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 ISMT_DESC_ENTRIES 32 /* number of descriptor entries */
  79. #define ISMT_MAX_RETRIES 3 /* number of SMBus retries to attempt */
  80. /* Hardware Descriptor Constants - Control Field */
  81. #define ISMT_DESC_CWRL 0x01 /* Command/Write Length */
  82. #define ISMT_DESC_BLK 0X04 /* Perform Block Transaction */
  83. #define ISMT_DESC_FAIR 0x08 /* Set fairness flag upon successful arbit. */
  84. #define ISMT_DESC_PEC 0x10 /* Packet Error Code */
  85. #define ISMT_DESC_I2C 0x20 /* I2C Enable */
  86. #define ISMT_DESC_INT 0x40 /* Interrupt */
  87. #define ISMT_DESC_SOE 0x80 /* Stop On Error */
  88. /* Hardware Descriptor Constants - Status Field */
  89. #define ISMT_DESC_SCS 0x01 /* Success */
  90. #define ISMT_DESC_DLTO 0x04 /* Data Low Time Out */
  91. #define ISMT_DESC_NAK 0x08 /* NAK Received */
  92. #define ISMT_DESC_CRC 0x10 /* CRC Error */
  93. #define ISMT_DESC_CLTO 0x20 /* Clock Low Time Out */
  94. #define ISMT_DESC_COL 0x40 /* Collisions */
  95. #define ISMT_DESC_LPR 0x80 /* Large Packet Received */
  96. /* Macros */
  97. #define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
  98. /* iSMT General Register address offsets (SMBBAR + <addr>) */
  99. #define ISMT_GR_GCTRL 0x000 /* General Control */
  100. #define ISMT_GR_SMTICL 0x008 /* SMT Interrupt Cause Location */
  101. #define ISMT_GR_ERRINTMSK 0x010 /* Error Interrupt Mask */
  102. #define ISMT_GR_ERRAERMSK 0x014 /* Error AER Mask */
  103. #define ISMT_GR_ERRSTS 0x018 /* Error Status */
  104. #define ISMT_GR_ERRINFO 0x01c /* Error Information */
  105. /* iSMT Master Registers */
  106. #define ISMT_MSTR_MDBA 0x100 /* Master Descriptor Base Address */
  107. #define ISMT_MSTR_MCTRL 0x108 /* Master Control */
  108. #define ISMT_MSTR_MSTS 0x10c /* Master Status */
  109. #define ISMT_MSTR_MDS 0x110 /* Master Descriptor Size */
  110. #define ISMT_MSTR_RPOLICY 0x114 /* Retry Policy */
  111. /* iSMT Miscellaneous Registers */
  112. #define ISMT_SPGT 0x300 /* SMBus PHY Global Timing */
  113. /* General Control Register (GCTRL) bit definitions */
  114. #define ISMT_GCTRL_TRST 0x04 /* Target Reset */
  115. #define ISMT_GCTRL_KILL 0x08 /* Kill */
  116. #define ISMT_GCTRL_SRST 0x40 /* Soft Reset */
  117. /* Master Control Register (MCTRL) bit definitions */
  118. #define ISMT_MCTRL_SS 0x01 /* Start/Stop */
  119. #define ISMT_MCTRL_MEIE 0x10 /* Master Error Interrupt Enable */
  120. #define ISMT_MCTRL_FMHP 0x00ff0000 /* Firmware Master Head Ptr (FMHP) */
  121. /* Master Status Register (MSTS) bit definitions */
  122. #define ISMT_MSTS_HMTP 0xff0000 /* HW Master Tail Pointer (HMTP) */
  123. #define ISMT_MSTS_MIS 0x20 /* Master Interrupt Status (MIS) */
  124. #define ISMT_MSTS_MEIS 0x10 /* Master Error Int Status (MEIS) */
  125. #define ISMT_MSTS_IP 0x01 /* In Progress */
  126. /* Master Descriptor Size (MDS) bit definitions */
  127. #define ISMT_MDS_MASK 0xff /* Master Descriptor Size mask (MDS) */
  128. /* SMBus PHY Global Timing Register (SPGT) bit definitions */
  129. #define ISMT_SPGT_SPD_MASK 0xc0000000 /* SMBus Speed mask */
  130. #define ISMT_SPGT_SPD_80K 0x00 /* 80 kHz */
  131. #define ISMT_SPGT_SPD_100K (0x1 << 30) /* 100 kHz */
  132. #define ISMT_SPGT_SPD_400K (0x2 << 30) /* 400 kHz */
  133. #define ISMT_SPGT_SPD_1M (0x3 << 30) /* 1 MHz */
  134. /* MSI Control Register (MSICTL) bit definitions */
  135. #define ISMT_MSICTL_MSIE 0x01 /* MSI Enable */
  136. /* iSMT Hardware Descriptor */
  137. struct ismt_desc {
  138. u8 tgtaddr_rw; /* target address & r/w bit */
  139. u8 wr_len_cmd; /* write length in bytes or a command */
  140. u8 rd_len; /* read length */
  141. u8 control; /* control bits */
  142. u8 status; /* status bits */
  143. u8 retry; /* collision retry and retry count */
  144. u8 rxbytes; /* received bytes */
  145. u8 txbytes; /* transmitted bytes */
  146. u32 dptr_low; /* lower 32 bit of the data pointer */
  147. u32 dptr_high; /* upper 32 bit of the data pointer */
  148. } __packed;
  149. struct ismt_priv {
  150. struct i2c_adapter adapter;
  151. void *smba; /* PCI BAR */
  152. struct pci_dev *pci_dev;
  153. struct ismt_desc *hw; /* descriptor virt base addr */
  154. dma_addr_t io_rng_dma; /* descriptor HW base addr */
  155. u8 head; /* ring buffer head pointer */
  156. struct completion cmp; /* interrupt completion */
  157. u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* temp R/W data buffer */
  158. bool using_msi; /* type of interrupt flag */
  159. };
  160. /**
  161. * ismt_ids - PCI device IDs supported by this driver
  162. */
  163. static const DEFINE_PCI_DEVICE_TABLE(ismt_ids) = {
  164. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
  165. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
  166. { 0, }
  167. };
  168. MODULE_DEVICE_TABLE(pci, ismt_ids);
  169. /* Bus speed control bits for slow debuggers - refer to the docs for usage */
  170. static unsigned int bus_speed;
  171. module_param(bus_speed, uint, S_IRUGO);
  172. MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
  173. /**
  174. * __ismt_desc_dump() - dump the contents of a specific descriptor
  175. */
  176. static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
  177. {
  178. dev_dbg(dev, "Descriptor struct: %p\n", desc);
  179. dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
  180. dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
  181. dev_dbg(dev, "\trd_len= 0x%02X\n", desc->rd_len);
  182. dev_dbg(dev, "\tcontrol= 0x%02X\n", desc->control);
  183. dev_dbg(dev, "\tstatus= 0x%02X\n", desc->status);
  184. dev_dbg(dev, "\tretry= 0x%02X\n", desc->retry);
  185. dev_dbg(dev, "\trxbytes= 0x%02X\n", desc->rxbytes);
  186. dev_dbg(dev, "\ttxbytes= 0x%02X\n", desc->txbytes);
  187. dev_dbg(dev, "\tdptr_low= 0x%08X\n", desc->dptr_low);
  188. dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
  189. }
  190. /**
  191. * ismt_desc_dump() - dump the contents of a descriptor for debug purposes
  192. * @priv: iSMT private data
  193. */
  194. static void ismt_desc_dump(struct ismt_priv *priv)
  195. {
  196. struct device *dev = &priv->pci_dev->dev;
  197. struct ismt_desc *desc = &priv->hw[priv->head];
  198. dev_dbg(dev, "Dump of the descriptor struct: 0x%X\n", priv->head);
  199. __ismt_desc_dump(dev, desc);
  200. }
  201. /**
  202. * ismt_gen_reg_dump() - dump the iSMT General Registers
  203. * @priv: iSMT private data
  204. */
  205. static void ismt_gen_reg_dump(struct ismt_priv *priv)
  206. {
  207. struct device *dev = &priv->pci_dev->dev;
  208. dev_dbg(dev, "Dump of the iSMT General Registers\n");
  209. dev_dbg(dev, " GCTRL.... : (0x%p)=0x%X\n",
  210. priv->smba + ISMT_GR_GCTRL,
  211. readl(priv->smba + ISMT_GR_GCTRL));
  212. dev_dbg(dev, " SMTICL... : (0x%p)=0x%016llX\n",
  213. priv->smba + ISMT_GR_SMTICL,
  214. (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
  215. dev_dbg(dev, " ERRINTMSK : (0x%p)=0x%X\n",
  216. priv->smba + ISMT_GR_ERRINTMSK,
  217. readl(priv->smba + ISMT_GR_ERRINTMSK));
  218. dev_dbg(dev, " ERRAERMSK : (0x%p)=0x%X\n",
  219. priv->smba + ISMT_GR_ERRAERMSK,
  220. readl(priv->smba + ISMT_GR_ERRAERMSK));
  221. dev_dbg(dev, " ERRSTS... : (0x%p)=0x%X\n",
  222. priv->smba + ISMT_GR_ERRSTS,
  223. readl(priv->smba + ISMT_GR_ERRSTS));
  224. dev_dbg(dev, " ERRINFO.. : (0x%p)=0x%X\n",
  225. priv->smba + ISMT_GR_ERRINFO,
  226. readl(priv->smba + ISMT_GR_ERRINFO));
  227. }
  228. /**
  229. * ismt_mstr_reg_dump() - dump the iSMT Master Registers
  230. * @priv: iSMT private data
  231. */
  232. static void ismt_mstr_reg_dump(struct ismt_priv *priv)
  233. {
  234. struct device *dev = &priv->pci_dev->dev;
  235. dev_dbg(dev, "Dump of the iSMT Master Registers\n");
  236. dev_dbg(dev, " MDBA..... : (0x%p)=0x%016llX\n",
  237. priv->smba + ISMT_MSTR_MDBA,
  238. (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
  239. dev_dbg(dev, " MCTRL.... : (0x%p)=0x%X\n",
  240. priv->smba + ISMT_MSTR_MCTRL,
  241. readl(priv->smba + ISMT_MSTR_MCTRL));
  242. dev_dbg(dev, " MSTS..... : (0x%p)=0x%X\n",
  243. priv->smba + ISMT_MSTR_MSTS,
  244. readl(priv->smba + ISMT_MSTR_MSTS));
  245. dev_dbg(dev, " MDS...... : (0x%p)=0x%X\n",
  246. priv->smba + ISMT_MSTR_MDS,
  247. readl(priv->smba + ISMT_MSTR_MDS));
  248. dev_dbg(dev, " RPOLICY.. : (0x%p)=0x%X\n",
  249. priv->smba + ISMT_MSTR_RPOLICY,
  250. readl(priv->smba + ISMT_MSTR_RPOLICY));
  251. dev_dbg(dev, " SPGT..... : (0x%p)=0x%X\n",
  252. priv->smba + ISMT_SPGT,
  253. readl(priv->smba + ISMT_SPGT));
  254. }
  255. /**
  256. * ismt_submit_desc() - add a descriptor to the ring
  257. * @priv: iSMT private data
  258. */
  259. static void ismt_submit_desc(struct ismt_priv *priv)
  260. {
  261. uint fmhp;
  262. uint val;
  263. ismt_desc_dump(priv);
  264. ismt_gen_reg_dump(priv);
  265. ismt_mstr_reg_dump(priv);
  266. /* Set the FMHP (Firmware Master Head Pointer)*/
  267. fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
  268. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  269. writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
  270. priv->smba + ISMT_MSTR_MCTRL);
  271. /* Set the start bit */
  272. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  273. writel(val | ISMT_MCTRL_SS,
  274. priv->smba + ISMT_MSTR_MCTRL);
  275. }
  276. /**
  277. * ismt_process_desc() - handle the completion of the descriptor
  278. * @desc: the iSMT hardware descriptor
  279. * @data: data buffer from the upper layer
  280. * @priv: ismt_priv struct holding our dma buffer
  281. * @size: SMBus transaction type
  282. * @read_write: flag to indicate if this is a read or write
  283. */
  284. static int ismt_process_desc(const struct ismt_desc *desc,
  285. union i2c_smbus_data *data,
  286. struct ismt_priv *priv, int size,
  287. char read_write)
  288. {
  289. u8 *dma_buffer = priv->dma_buffer;
  290. dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
  291. __ismt_desc_dump(&priv->pci_dev->dev, desc);
  292. if (desc->status & ISMT_DESC_SCS) {
  293. if (read_write == I2C_SMBUS_WRITE &&
  294. size != I2C_SMBUS_PROC_CALL)
  295. return 0;
  296. switch (size) {
  297. case I2C_SMBUS_BYTE:
  298. case I2C_SMBUS_BYTE_DATA:
  299. data->byte = dma_buffer[0];
  300. break;
  301. case I2C_SMBUS_WORD_DATA:
  302. case I2C_SMBUS_PROC_CALL:
  303. data->word = dma_buffer[0] | (dma_buffer[1] << 8);
  304. break;
  305. case I2C_SMBUS_BLOCK_DATA:
  306. memcpy(&data->block[1], dma_buffer, desc->rxbytes);
  307. data->block[0] = desc->rxbytes;
  308. break;
  309. }
  310. return 0;
  311. }
  312. if (likely(desc->status & ISMT_DESC_NAK))
  313. return -ENXIO;
  314. if (desc->status & ISMT_DESC_CRC)
  315. return -EBADMSG;
  316. if (desc->status & ISMT_DESC_COL)
  317. return -EAGAIN;
  318. if (desc->status & ISMT_DESC_LPR)
  319. return -EPROTO;
  320. if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
  321. return -ETIMEDOUT;
  322. return -EIO;
  323. }
  324. /**
  325. * ismt_access() - process an SMBus command
  326. * @adap: the i2c host adapter
  327. * @addr: address of the i2c/SMBus target
  328. * @flags: command options
  329. * @read_write: read from or write to device
  330. * @command: the i2c/SMBus command to issue
  331. * @size: SMBus transaction type
  332. * @data: read/write data buffer
  333. */
  334. static int ismt_access(struct i2c_adapter *adap, u16 addr,
  335. unsigned short flags, char read_write, u8 command,
  336. int size, union i2c_smbus_data *data)
  337. {
  338. int ret;
  339. dma_addr_t dma_addr = 0; /* address of the data buffer */
  340. u8 dma_size = 0;
  341. enum dma_data_direction dma_direction = 0;
  342. struct ismt_desc *desc;
  343. struct ismt_priv *priv = i2c_get_adapdata(adap);
  344. struct device *dev = &priv->pci_dev->dev;
  345. desc = &priv->hw[priv->head];
  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);
  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. default:
  451. dev_err(dev, "Unsupported transaction %d\n",
  452. size);
  453. return -EOPNOTSUPP;
  454. }
  455. /* map the data buffer */
  456. if (dma_size != 0) {
  457. dev_dbg(dev, " dev=%p\n", dev);
  458. dev_dbg(dev, " data=%p\n", data);
  459. dev_dbg(dev, " dma_buffer=%p\n", priv->dma_buffer);
  460. dev_dbg(dev, " dma_size=%d\n", dma_size);
  461. dev_dbg(dev, " dma_direction=%d\n", dma_direction);
  462. dma_addr = dma_map_single(dev,
  463. priv->dma_buffer,
  464. dma_size,
  465. dma_direction);
  466. if (dma_mapping_error(dev, dma_addr)) {
  467. dev_err(dev, "Error in mapping dma buffer %p\n",
  468. priv->dma_buffer);
  469. return -EIO;
  470. }
  471. dev_dbg(dev, " dma_addr = 0x%016llX\n",
  472. (unsigned long long)dma_addr);
  473. desc->dptr_low = lower_32_bits(dma_addr);
  474. desc->dptr_high = upper_32_bits(dma_addr);
  475. }
  476. INIT_COMPLETION(priv->cmp);
  477. /* Add the descriptor */
  478. ismt_submit_desc(priv);
  479. /* Now we wait for interrupt completion, 1s */
  480. ret = wait_for_completion_timeout(&priv->cmp, HZ*1);
  481. /* unmap the data buffer */
  482. if (dma_size != 0)
  483. dma_unmap_single(&adap->dev, dma_addr, dma_size, dma_direction);
  484. if (unlikely(!ret)) {
  485. dev_err(dev, "completion wait timed out\n");
  486. ret = -ETIMEDOUT;
  487. goto out;
  488. }
  489. /* do any post processing of the descriptor here */
  490. ret = ismt_process_desc(desc, data, priv, size, read_write);
  491. out:
  492. /* Update the ring pointer */
  493. priv->head++;
  494. priv->head %= ISMT_DESC_ENTRIES;
  495. return ret;
  496. }
  497. /**
  498. * ismt_func() - report which i2c commands are supported by this adapter
  499. * @adap: the i2c host adapter
  500. */
  501. static u32 ismt_func(struct i2c_adapter *adap)
  502. {
  503. return I2C_FUNC_SMBUS_QUICK |
  504. I2C_FUNC_SMBUS_BYTE |
  505. I2C_FUNC_SMBUS_BYTE_DATA |
  506. I2C_FUNC_SMBUS_WORD_DATA |
  507. I2C_FUNC_SMBUS_PROC_CALL |
  508. I2C_FUNC_SMBUS_BLOCK_DATA |
  509. I2C_FUNC_SMBUS_PEC;
  510. }
  511. /**
  512. * smbus_algorithm - the adapter algorithm and supported functionality
  513. * @smbus_xfer: the adapter algorithm
  514. * @functionality: functionality supported by the adapter
  515. */
  516. static const struct i2c_algorithm smbus_algorithm = {
  517. .smbus_xfer = ismt_access,
  518. .functionality = ismt_func,
  519. };
  520. /**
  521. * ismt_handle_isr() - interrupt handler bottom half
  522. * @priv: iSMT private data
  523. */
  524. static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
  525. {
  526. complete(&priv->cmp);
  527. return IRQ_HANDLED;
  528. }
  529. /**
  530. * ismt_do_interrupt() - IRQ interrupt handler
  531. * @vec: interrupt vector
  532. * @data: iSMT private data
  533. */
  534. static irqreturn_t ismt_do_interrupt(int vec, void *data)
  535. {
  536. u32 val;
  537. struct ismt_priv *priv = data;
  538. /*
  539. * check to see it's our interrupt, return IRQ_NONE if not ours
  540. * since we are sharing interrupt
  541. */
  542. val = readl(priv->smba + ISMT_MSTR_MSTS);
  543. if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
  544. return IRQ_NONE;
  545. else
  546. writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
  547. priv->smba + ISMT_MSTR_MSTS);
  548. return ismt_handle_isr(priv);
  549. }
  550. /**
  551. * ismt_do_msi_interrupt() - MSI interrupt handler
  552. * @vec: interrupt vector
  553. * @data: iSMT private data
  554. */
  555. static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
  556. {
  557. return ismt_handle_isr(data);
  558. }
  559. /**
  560. * ismt_hw_init() - initialize the iSMT hardware
  561. * @priv: iSMT private data
  562. */
  563. static void ismt_hw_init(struct ismt_priv *priv)
  564. {
  565. u32 val;
  566. struct device *dev = &priv->pci_dev->dev;
  567. /* initialize the Master Descriptor Base Address (MDBA) */
  568. writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
  569. /* initialize the Master Control Register (MCTRL) */
  570. writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
  571. /* initialize the Master Status Register (MSTS) */
  572. writel(0, priv->smba + ISMT_MSTR_MSTS);
  573. /* initialize the Master Descriptor Size (MDS) */
  574. val = readl(priv->smba + ISMT_MSTR_MDS);
  575. writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
  576. priv->smba + ISMT_MSTR_MDS);
  577. /*
  578. * Set the SMBus speed (could use this for slow HW debuggers)
  579. */
  580. val = readl(priv->smba + ISMT_SPGT);
  581. switch (bus_speed) {
  582. case 0:
  583. break;
  584. case 80:
  585. dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
  586. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
  587. priv->smba + ISMT_SPGT);
  588. break;
  589. case 100:
  590. dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
  591. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
  592. priv->smba + ISMT_SPGT);
  593. break;
  594. case 400:
  595. dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
  596. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
  597. priv->smba + ISMT_SPGT);
  598. break;
  599. case 1000:
  600. dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
  601. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
  602. priv->smba + ISMT_SPGT);
  603. break;
  604. default:
  605. dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
  606. break;
  607. }
  608. val = readl(priv->smba + ISMT_SPGT);
  609. switch (val & ISMT_SPGT_SPD_MASK) {
  610. case ISMT_SPGT_SPD_80K:
  611. bus_speed = 80;
  612. break;
  613. case ISMT_SPGT_SPD_100K:
  614. bus_speed = 100;
  615. break;
  616. case ISMT_SPGT_SPD_400K:
  617. bus_speed = 400;
  618. break;
  619. case ISMT_SPGT_SPD_1M:
  620. bus_speed = 1000;
  621. break;
  622. }
  623. dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
  624. }
  625. /**
  626. * ismt_dev_init() - initialize the iSMT data structures
  627. * @priv: iSMT private data
  628. */
  629. static int ismt_dev_init(struct ismt_priv *priv)
  630. {
  631. /* allocate memory for the descriptor */
  632. priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
  633. (ISMT_DESC_ENTRIES
  634. * sizeof(struct ismt_desc)),
  635. &priv->io_rng_dma,
  636. GFP_KERNEL);
  637. if (!priv->hw)
  638. return -ENOMEM;
  639. memset(priv->hw, 0, (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc)));
  640. priv->head = 0;
  641. init_completion(&priv->cmp);
  642. return 0;
  643. }
  644. /**
  645. * ismt_int_init() - initialize interrupts
  646. * @priv: iSMT private data
  647. */
  648. static int ismt_int_init(struct ismt_priv *priv)
  649. {
  650. int err;
  651. /* Try using MSI interrupts */
  652. err = pci_enable_msi(priv->pci_dev);
  653. if (err) {
  654. dev_warn(&priv->pci_dev->dev,
  655. "Unable to use MSI interrupts, falling back to legacy\n");
  656. goto intx;
  657. }
  658. err = devm_request_irq(&priv->pci_dev->dev,
  659. priv->pci_dev->irq,
  660. ismt_do_msi_interrupt,
  661. 0,
  662. "ismt-msi",
  663. priv);
  664. if (err) {
  665. pci_disable_msi(priv->pci_dev);
  666. goto intx;
  667. }
  668. priv->using_msi = true;
  669. goto done;
  670. /* Try using legacy interrupts */
  671. intx:
  672. err = devm_request_irq(&priv->pci_dev->dev,
  673. priv->pci_dev->irq,
  674. ismt_do_interrupt,
  675. IRQF_SHARED,
  676. "ismt-intx",
  677. priv);
  678. if (err) {
  679. dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
  680. return -ENODEV;
  681. }
  682. priv->using_msi = false;
  683. done:
  684. return 0;
  685. }
  686. static struct pci_driver ismt_driver;
  687. /**
  688. * ismt_probe() - probe for iSMT devices
  689. * @pdev: PCI-Express device
  690. * @id: PCI-Express device ID
  691. */
  692. static int
  693. ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  694. {
  695. int err;
  696. struct ismt_priv *priv;
  697. unsigned long start, len;
  698. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  699. if (!priv)
  700. return -ENOMEM;
  701. pci_set_drvdata(pdev, priv);
  702. i2c_set_adapdata(&priv->adapter, priv);
  703. priv->adapter.owner = THIS_MODULE;
  704. priv->adapter.class = I2C_CLASS_HWMON;
  705. priv->adapter.algo = &smbus_algorithm;
  706. /* set up the sysfs linkage to our parent device */
  707. priv->adapter.dev.parent = &pdev->dev;
  708. /* number of retries on lost arbitration */
  709. priv->adapter.retries = ISMT_MAX_RETRIES;
  710. priv->pci_dev = pdev;
  711. err = pcim_enable_device(pdev);
  712. if (err) {
  713. dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
  714. err);
  715. return err;
  716. }
  717. /* enable bus mastering */
  718. pci_set_master(pdev);
  719. /* Determine the address of the SMBus area */
  720. start = pci_resource_start(pdev, SMBBAR);
  721. len = pci_resource_len(pdev, SMBBAR);
  722. if (!start || !len) {
  723. dev_err(&pdev->dev,
  724. "SMBus base address uninitialized, upgrade BIOS\n");
  725. return -ENODEV;
  726. }
  727. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  728. "SMBus iSMT adapter at %lx", start);
  729. dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
  730. dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
  731. err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
  732. if (err) {
  733. dev_err(&pdev->dev, "ACPI resource conflict!\n");
  734. return err;
  735. }
  736. err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
  737. if (err) {
  738. dev_err(&pdev->dev,
  739. "Failed to request SMBus region 0x%lx-0x%lx\n",
  740. start, start + len);
  741. return err;
  742. }
  743. priv->smba = pcim_iomap(pdev, SMBBAR, len);
  744. if (!priv->smba) {
  745. dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
  746. err = -ENODEV;
  747. goto fail;
  748. }
  749. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
  750. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
  751. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  752. (pci_set_consistent_dma_mask(pdev,
  753. DMA_BIT_MASK(32)) != 0)) {
  754. dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
  755. pdev);
  756. goto fail;
  757. }
  758. }
  759. err = ismt_dev_init(priv);
  760. if (err)
  761. goto fail;
  762. ismt_hw_init(priv);
  763. err = ismt_int_init(priv);
  764. if (err)
  765. goto fail;
  766. err = i2c_add_adapter(&priv->adapter);
  767. if (err) {
  768. dev_err(&pdev->dev, "Failed to add SMBus iSMT adapter\n");
  769. err = -ENODEV;
  770. goto fail;
  771. }
  772. return 0;
  773. fail:
  774. pci_release_region(pdev, SMBBAR);
  775. return err;
  776. }
  777. /**
  778. * ismt_remove() - release driver resources
  779. * @pdev: PCI-Express device
  780. */
  781. static void ismt_remove(struct pci_dev *pdev)
  782. {
  783. struct ismt_priv *priv = pci_get_drvdata(pdev);
  784. i2c_del_adapter(&priv->adapter);
  785. pci_release_region(pdev, SMBBAR);
  786. }
  787. /**
  788. * ismt_suspend() - place the device in suspend
  789. * @pdev: PCI-Express device
  790. * @mesg: PM message
  791. */
  792. #ifdef CONFIG_PM
  793. static int ismt_suspend(struct pci_dev *pdev, pm_message_t mesg)
  794. {
  795. pci_save_state(pdev);
  796. pci_set_power_state(pdev, pci_choose_state(pdev, mesg));
  797. return 0;
  798. }
  799. /**
  800. * ismt_resume() - PCI resume code
  801. * @pdev: PCI-Express device
  802. */
  803. static int ismt_resume(struct pci_dev *pdev)
  804. {
  805. pci_set_power_state(pdev, PCI_D0);
  806. pci_restore_state(pdev);
  807. return pci_enable_device(pdev);
  808. }
  809. #else
  810. #define ismt_suspend NULL
  811. #define ismt_resume NULL
  812. #endif
  813. static struct pci_driver ismt_driver = {
  814. .name = "ismt_smbus",
  815. .id_table = ismt_ids,
  816. .probe = ismt_probe,
  817. .remove = ismt_remove,
  818. .suspend = ismt_suspend,
  819. .resume = ismt_resume,
  820. };
  821. module_pci_driver(ismt_driver);
  822. MODULE_LICENSE("Dual BSD/GPL");
  823. MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
  824. MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");