xgbe-i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2016 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2016 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/module.h>
  117. #include <linux/kmod.h>
  118. #include <linux/delay.h>
  119. #include <linux/completion.h>
  120. #include <linux/mutex.h>
  121. #include "xgbe.h"
  122. #include "xgbe-common.h"
  123. #define XGBE_ABORT_COUNT 500
  124. #define XGBE_DISABLE_COUNT 1000
  125. #define XGBE_STD_SPEED 1
  126. #define XGBE_INTR_RX_FULL BIT(IC_RAW_INTR_STAT_RX_FULL_INDEX)
  127. #define XGBE_INTR_TX_EMPTY BIT(IC_RAW_INTR_STAT_TX_EMPTY_INDEX)
  128. #define XGBE_INTR_TX_ABRT BIT(IC_RAW_INTR_STAT_TX_ABRT_INDEX)
  129. #define XGBE_INTR_STOP_DET BIT(IC_RAW_INTR_STAT_STOP_DET_INDEX)
  130. #define XGBE_DEFAULT_INT_MASK (XGBE_INTR_RX_FULL | \
  131. XGBE_INTR_TX_EMPTY | \
  132. XGBE_INTR_TX_ABRT | \
  133. XGBE_INTR_STOP_DET)
  134. #define XGBE_I2C_READ BIT(8)
  135. #define XGBE_I2C_STOP BIT(9)
  136. static int xgbe_i2c_abort(struct xgbe_prv_data *pdata)
  137. {
  138. unsigned int wait = XGBE_ABORT_COUNT;
  139. /* Must be enabled to recognize the abort request */
  140. XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, 1);
  141. /* Issue the abort */
  142. XI2C_IOWRITE_BITS(pdata, IC_ENABLE, ABORT, 1);
  143. while (wait--) {
  144. if (!XI2C_IOREAD_BITS(pdata, IC_ENABLE, ABORT))
  145. return 0;
  146. usleep_range(500, 600);
  147. }
  148. return -EBUSY;
  149. }
  150. static int xgbe_i2c_set_enable(struct xgbe_prv_data *pdata, bool enable)
  151. {
  152. unsigned int wait = XGBE_DISABLE_COUNT;
  153. unsigned int mode = enable ? 1 : 0;
  154. while (wait--) {
  155. XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, mode);
  156. if (XI2C_IOREAD_BITS(pdata, IC_ENABLE_STATUS, EN) == mode)
  157. return 0;
  158. usleep_range(100, 110);
  159. }
  160. return -EBUSY;
  161. }
  162. static int xgbe_i2c_disable(struct xgbe_prv_data *pdata)
  163. {
  164. unsigned int ret;
  165. ret = xgbe_i2c_set_enable(pdata, false);
  166. if (ret) {
  167. /* Disable failed, try an abort */
  168. ret = xgbe_i2c_abort(pdata);
  169. if (ret)
  170. return ret;
  171. /* Abort succeeded, try to disable again */
  172. ret = xgbe_i2c_set_enable(pdata, false);
  173. }
  174. return ret;
  175. }
  176. static int xgbe_i2c_enable(struct xgbe_prv_data *pdata)
  177. {
  178. return xgbe_i2c_set_enable(pdata, true);
  179. }
  180. static void xgbe_i2c_clear_all_interrupts(struct xgbe_prv_data *pdata)
  181. {
  182. XI2C_IOREAD(pdata, IC_CLR_INTR);
  183. }
  184. static void xgbe_i2c_disable_interrupts(struct xgbe_prv_data *pdata)
  185. {
  186. XI2C_IOWRITE(pdata, IC_INTR_MASK, 0);
  187. }
  188. static void xgbe_i2c_enable_interrupts(struct xgbe_prv_data *pdata)
  189. {
  190. XI2C_IOWRITE(pdata, IC_INTR_MASK, XGBE_DEFAULT_INT_MASK);
  191. }
  192. static void xgbe_i2c_write(struct xgbe_prv_data *pdata)
  193. {
  194. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  195. unsigned int tx_slots;
  196. unsigned int cmd;
  197. /* Configured to never receive Rx overflows, so fill up Tx fifo */
  198. tx_slots = pdata->i2c.tx_fifo_size - XI2C_IOREAD(pdata, IC_TXFLR);
  199. while (tx_slots && state->tx_len) {
  200. if (state->op->cmd == XGBE_I2C_CMD_READ)
  201. cmd = XGBE_I2C_READ;
  202. else
  203. cmd = *state->tx_buf++;
  204. if (state->tx_len == 1)
  205. XI2C_SET_BITS(cmd, IC_DATA_CMD, STOP, 1);
  206. XI2C_IOWRITE(pdata, IC_DATA_CMD, cmd);
  207. tx_slots--;
  208. state->tx_len--;
  209. }
  210. /* No more Tx operations, so ignore TX_EMPTY and return */
  211. if (!state->tx_len)
  212. XI2C_IOWRITE_BITS(pdata, IC_INTR_MASK, TX_EMPTY, 0);
  213. }
  214. static void xgbe_i2c_read(struct xgbe_prv_data *pdata)
  215. {
  216. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  217. unsigned int rx_slots;
  218. /* Anything to be read? */
  219. if (state->op->cmd != XGBE_I2C_CMD_READ)
  220. return;
  221. rx_slots = XI2C_IOREAD(pdata, IC_RXFLR);
  222. while (rx_slots && state->rx_len) {
  223. *state->rx_buf++ = XI2C_IOREAD(pdata, IC_DATA_CMD);
  224. state->rx_len--;
  225. rx_slots--;
  226. }
  227. }
  228. static void xgbe_i2c_clear_isr_interrupts(struct xgbe_prv_data *pdata,
  229. unsigned int isr)
  230. {
  231. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  232. if (isr & XGBE_INTR_TX_ABRT) {
  233. state->tx_abort_source = XI2C_IOREAD(pdata, IC_TX_ABRT_SOURCE);
  234. XI2C_IOREAD(pdata, IC_CLR_TX_ABRT);
  235. }
  236. if (isr & XGBE_INTR_STOP_DET)
  237. XI2C_IOREAD(pdata, IC_CLR_STOP_DET);
  238. }
  239. static irqreturn_t xgbe_i2c_isr(int irq, void *data)
  240. {
  241. struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
  242. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  243. unsigned int isr;
  244. isr = XI2C_IOREAD(pdata, IC_RAW_INTR_STAT);
  245. netif_dbg(pdata, intr, pdata->netdev,
  246. "I2C interrupt received: status=%#010x\n", isr);
  247. xgbe_i2c_clear_isr_interrupts(pdata, isr);
  248. if (isr & XGBE_INTR_TX_ABRT) {
  249. netif_dbg(pdata, link, pdata->netdev,
  250. "I2C TX_ABRT received (%#010x) for target %#04x\n",
  251. state->tx_abort_source, state->op->target);
  252. xgbe_i2c_disable_interrupts(pdata);
  253. state->ret = -EIO;
  254. goto out;
  255. }
  256. /* Check for data in the Rx fifo */
  257. xgbe_i2c_read(pdata);
  258. /* Fill up the Tx fifo next */
  259. xgbe_i2c_write(pdata);
  260. out:
  261. /* Complete on an error or STOP condition */
  262. if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET))
  263. complete(&pdata->i2c_complete);
  264. return IRQ_HANDLED;
  265. }
  266. static void xgbe_i2c_set_mode(struct xgbe_prv_data *pdata)
  267. {
  268. unsigned int reg;
  269. reg = XI2C_IOREAD(pdata, IC_CON);
  270. XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1);
  271. XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1);
  272. XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1);
  273. XI2C_SET_BITS(reg, IC_CON, SPEED, XGBE_STD_SPEED);
  274. XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1);
  275. XI2C_IOWRITE(pdata, IC_CON, reg);
  276. }
  277. static void xgbe_i2c_get_features(struct xgbe_prv_data *pdata)
  278. {
  279. struct xgbe_i2c *i2c = &pdata->i2c;
  280. unsigned int reg;
  281. reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1);
  282. i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  283. MAX_SPEED_MODE);
  284. i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  285. RX_BUFFER_DEPTH);
  286. i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
  287. TX_BUFFER_DEPTH);
  288. if (netif_msg_probe(pdata))
  289. dev_dbg(pdata->dev, "I2C features: %s=%u, %s=%u, %s=%u\n",
  290. "MAX_SPEED_MODE", i2c->max_speed_mode,
  291. "RX_BUFFER_DEPTH", i2c->rx_fifo_size,
  292. "TX_BUFFER_DEPTH", i2c->tx_fifo_size);
  293. }
  294. static void xgbe_i2c_set_target(struct xgbe_prv_data *pdata, unsigned int addr)
  295. {
  296. XI2C_IOWRITE(pdata, IC_TAR, addr);
  297. }
  298. static irqreturn_t xgbe_i2c_combined_isr(int irq, struct xgbe_prv_data *pdata)
  299. {
  300. if (!XI2C_IOREAD(pdata, IC_RAW_INTR_STAT))
  301. return IRQ_HANDLED;
  302. return xgbe_i2c_isr(irq, pdata);
  303. }
  304. static int xgbe_i2c_xfer(struct xgbe_prv_data *pdata, struct xgbe_i2c_op *op)
  305. {
  306. struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;
  307. int ret;
  308. mutex_lock(&pdata->i2c_mutex);
  309. reinit_completion(&pdata->i2c_complete);
  310. ret = xgbe_i2c_disable(pdata);
  311. if (ret) {
  312. netdev_err(pdata->netdev, "failed to disable i2c master\n");
  313. goto unlock;
  314. }
  315. xgbe_i2c_set_target(pdata, op->target);
  316. memset(state, 0, sizeof(*state));
  317. state->op = op;
  318. state->tx_len = op->len;
  319. state->tx_buf = op->buf;
  320. state->rx_len = op->len;
  321. state->rx_buf = op->buf;
  322. xgbe_i2c_clear_all_interrupts(pdata);
  323. ret = xgbe_i2c_enable(pdata);
  324. if (ret) {
  325. netdev_err(pdata->netdev, "failed to enable i2c master\n");
  326. goto unlock;
  327. }
  328. /* Enabling the interrupts will cause the TX FIFO empty interrupt to
  329. * fire and begin to process the command via the ISR.
  330. */
  331. xgbe_i2c_enable_interrupts(pdata);
  332. if (!wait_for_completion_timeout(&pdata->i2c_complete, HZ)) {
  333. netdev_err(pdata->netdev, "i2c operation timed out\n");
  334. ret = -ETIMEDOUT;
  335. goto disable;
  336. }
  337. ret = state->ret;
  338. if (ret) {
  339. if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK)
  340. ret = -ENOTCONN;
  341. else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST)
  342. ret = -EAGAIN;
  343. }
  344. disable:
  345. xgbe_i2c_disable_interrupts(pdata);
  346. xgbe_i2c_disable(pdata);
  347. unlock:
  348. mutex_unlock(&pdata->i2c_mutex);
  349. return ret;
  350. }
  351. static void xgbe_i2c_stop(struct xgbe_prv_data *pdata)
  352. {
  353. if (!pdata->i2c.started)
  354. return;
  355. netif_dbg(pdata, link, pdata->netdev, "stopping I2C\n");
  356. pdata->i2c.started = 0;
  357. xgbe_i2c_disable_interrupts(pdata);
  358. xgbe_i2c_disable(pdata);
  359. xgbe_i2c_clear_all_interrupts(pdata);
  360. if (pdata->dev_irq != pdata->i2c_irq)
  361. devm_free_irq(pdata->dev, pdata->i2c_irq, pdata);
  362. }
  363. static int xgbe_i2c_start(struct xgbe_prv_data *pdata)
  364. {
  365. int ret;
  366. if (pdata->i2c.started)
  367. return 0;
  368. netif_dbg(pdata, link, pdata->netdev, "starting I2C\n");
  369. /* If we have a separate I2C irq, enable it */
  370. if (pdata->dev_irq != pdata->i2c_irq) {
  371. ret = devm_request_irq(pdata->dev, pdata->i2c_irq,
  372. xgbe_i2c_isr, 0, pdata->i2c_name,
  373. pdata);
  374. if (ret) {
  375. netdev_err(pdata->netdev, "i2c irq request failed\n");
  376. return ret;
  377. }
  378. }
  379. pdata->i2c.started = 1;
  380. return 0;
  381. }
  382. static int xgbe_i2c_init(struct xgbe_prv_data *pdata)
  383. {
  384. int ret;
  385. xgbe_i2c_disable_interrupts(pdata);
  386. ret = xgbe_i2c_disable(pdata);
  387. if (ret) {
  388. dev_err(pdata->dev, "failed to disable i2c master\n");
  389. return ret;
  390. }
  391. xgbe_i2c_get_features(pdata);
  392. xgbe_i2c_set_mode(pdata);
  393. xgbe_i2c_clear_all_interrupts(pdata);
  394. return 0;
  395. }
  396. void xgbe_init_function_ptrs_i2c(struct xgbe_i2c_if *i2c_if)
  397. {
  398. i2c_if->i2c_init = xgbe_i2c_init;
  399. i2c_if->i2c_start = xgbe_i2c_start;
  400. i2c_if->i2c_stop = xgbe_i2c_stop;
  401. i2c_if->i2c_xfer = xgbe_i2c_xfer;
  402. i2c_if->i2c_isr = xgbe_i2c_combined_isr;
  403. }