fu740_pci_dw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright 2021 Jessica Clarke <jrtc27@FreeBSD.org>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. */
  28. /* SiFive FU740 DesignWare PCIe driver */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/bus.h>
  32. #include <sys/gpio.h>
  33. #include <sys/kernel.h>
  34. #include <sys/module.h>
  35. #include <sys/rman.h>
  36. #include <machine/bus.h>
  37. #include <machine/intr.h>
  38. #include <machine/resource.h>
  39. #include <dev/clk/clk.h>
  40. #include <dev/hwreset/hwreset.h>
  41. #include <dev/gpio/gpiobusvar.h>
  42. #include <dev/ofw/ofw_bus.h>
  43. #include <dev/ofw/ofw_bus_subr.h>
  44. #include <dev/ofw/ofw_pci.h>
  45. #include <dev/ofw/ofwpci.h>
  46. #include <dev/pci/pcivar.h>
  47. #include <dev/pci/pcireg.h>
  48. #include <dev/pci/pcib_private.h>
  49. #include <dev/pci/pci_dw.h>
  50. #include "pcib_if.h"
  51. #include "pci_dw_if.h"
  52. #define FUDW_PHYS 2
  53. #define FUDW_LANES_PER_PHY 4
  54. #define FUDW_MGMT_PERST_N 0x0
  55. #define FUDW_MGMT_LTSSM_EN 0x10
  56. #define FUDW_MGMT_HOLD_PHY_RST 0x18
  57. #define FUDW_MGMT_DEVICE_TYPE 0x708
  58. #define FUDW_MGMT_DEVICE_TYPE_RC 0x4
  59. #define FUDW_MGMT_PHY_CR_PARA_REG(_n, _r) \
  60. (0x860 + (_n) * 0x40 + FUDW_MGMT_PHY_CR_PARA_##_r)
  61. #define FUDW_MGMT_PHY_CR_PARA_ADDR 0x0
  62. #define FUDW_MGMT_PHY_CR_PARA_READ_EN 0x10
  63. #define FUDW_MGMT_PHY_CR_PARA_READ_DATA 0x18
  64. #define FUDW_MGMT_PHY_CR_PARA_SEL 0x20
  65. #define FUDW_MGMT_PHY_CR_PARA_WRITE_DATA 0x28
  66. #define FUDW_MGMT_PHY_CR_PARA_WRITE_EN 0x30
  67. #define FUDW_MGMT_PHY_CR_PARA_ACK 0x38
  68. #define FUDW_MGMT_PHY_LANE(_n) (0x1008 + (_n) * 0x100)
  69. #define FUDW_MGMT_PHY_LANE_CDR_TRACK_EN (1 << 0)
  70. #define FUDW_MGMT_PHY_LANE_LOS_THRESH (1 << 5)
  71. #define FUDW_MGMT_PHY_LANE_TERM_EN (1 << 9)
  72. #define FUDW_MGMT_PHY_LANE_TERM_ACDC (1 << 10)
  73. #define FUDW_MGMT_PHY_LANE_EN (1 << 11)
  74. #define FUDW_MGMT_PHY_LANE_INIT \
  75. (FUDW_MGMT_PHY_LANE_CDR_TRACK_EN | FUDW_MGMT_PHY_LANE_LOS_THRESH | \
  76. FUDW_MGMT_PHY_LANE_TERM_EN | FUDW_MGMT_PHY_LANE_TERM_ACDC | \
  77. FUDW_MGMT_PHY_LANE_EN)
  78. #define FUDW_DBI_PORT_DBG1 0x72c
  79. #define FUDW_DBI_PORT_DBG1_LINK_UP (1 << 4)
  80. #define FUDW_DBI_PORT_DBG1_LINK_IN_TRAINING (1 << 29)
  81. struct fupci_softc {
  82. struct pci_dw_softc dw_sc;
  83. device_t dev;
  84. struct resource *mgmt_res;
  85. gpio_pin_t porst_pin;
  86. gpio_pin_t pwren_pin;
  87. clk_t pcie_aux_clk;
  88. hwreset_t pcie_aux_rst;
  89. };
  90. #define FUDW_MGMT_READ(_sc, _o) bus_read_4((_sc)->mgmt_res, (_o))
  91. #define FUDW_MGMT_WRITE(_sc, _o, _v) bus_write_4((_sc)->mgmt_res, (_o), (_v))
  92. static struct ofw_compat_data compat_data[] = {
  93. { "sifive,fu740-pcie", 1 },
  94. { NULL, 0 },
  95. };
  96. /* Currently unused; included for completeness */
  97. static int __unused
  98. fupci_phy_read(struct fupci_softc *sc, int phy, uint32_t reg, uint32_t *val)
  99. {
  100. unsigned timeout;
  101. uint32_t ack;
  102. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ADDR), reg);
  103. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_EN), 1);
  104. timeout = 10;
  105. do {
  106. ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
  107. if (ack != 0)
  108. break;
  109. DELAY(10);
  110. } while (--timeout > 0);
  111. if (timeout == 0) {
  112. device_printf(sc->dev, "Timeout waiting for read ACK\n");
  113. return (ETIMEDOUT);
  114. }
  115. *val = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_DATA));
  116. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_EN), 0);
  117. timeout = 10;
  118. do {
  119. ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
  120. if (ack == 0)
  121. break;
  122. DELAY(10);
  123. } while (--timeout > 0);
  124. if (timeout == 0) {
  125. device_printf(sc->dev, "Timeout waiting for read un-ACK\n");
  126. return (ETIMEDOUT);
  127. }
  128. return (0);
  129. }
  130. static int
  131. fupci_phy_write(struct fupci_softc *sc, int phy, uint32_t reg, uint32_t val)
  132. {
  133. unsigned timeout;
  134. uint32_t ack;
  135. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ADDR), reg);
  136. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_DATA), val);
  137. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_EN), 1);
  138. timeout = 10;
  139. do {
  140. ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
  141. if (ack != 0)
  142. break;
  143. DELAY(10);
  144. } while (--timeout > 0);
  145. if (timeout == 0) {
  146. device_printf(sc->dev, "Timeout waiting for write ACK\n");
  147. return (ETIMEDOUT);
  148. }
  149. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_EN), 0);
  150. timeout = 10;
  151. do {
  152. ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
  153. if (ack == 0)
  154. break;
  155. DELAY(10);
  156. } while (--timeout > 0);
  157. if (timeout == 0) {
  158. device_printf(sc->dev, "Timeout waiting for write un-ACK\n");
  159. return (ETIMEDOUT);
  160. }
  161. return (0);
  162. }
  163. static int
  164. fupci_phy_init(struct fupci_softc *sc)
  165. {
  166. device_t dev;
  167. int error, phy, lane;
  168. dev = sc->dev;
  169. /* Assert core power-on reset (active low) */
  170. error = gpio_pin_set_active(sc->porst_pin, false);
  171. if (error != 0) {
  172. device_printf(dev, "Cannot assert power-on reset: %d\n",
  173. error);
  174. return (error);
  175. }
  176. /* Assert PERST_N */
  177. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PERST_N, 0);
  178. /* Enable power */
  179. error = gpio_pin_set_active(sc->pwren_pin, true);
  180. if (error != 0) {
  181. device_printf(dev, "Cannot enable power: %d\n", error);
  182. return (error);
  183. }
  184. /* Deassert core power-on reset (active low) */
  185. error = gpio_pin_set_active(sc->porst_pin, true);
  186. if (error != 0) {
  187. device_printf(dev, "Cannot deassert power-on reset: %d\n",
  188. error);
  189. return (error);
  190. }
  191. /* Enable the aux clock */
  192. error = clk_enable(sc->pcie_aux_clk);
  193. if (error != 0) {
  194. device_printf(dev, "Cannot enable aux clock: %d\n", error);
  195. return (error);
  196. }
  197. /* Hold LTSSM in reset whilst initialising the PHYs */
  198. FUDW_MGMT_WRITE(sc, FUDW_MGMT_HOLD_PHY_RST, 1);
  199. /* Deassert the aux reset */
  200. error = hwreset_deassert(sc->pcie_aux_rst);
  201. if (error != 0) {
  202. device_printf(dev, "Cannot deassert aux reset: %d\n", error);
  203. return (error);
  204. }
  205. /* Enable control register interface */
  206. for (phy = 0; phy < FUDW_PHYS; ++phy)
  207. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, SEL), 1);
  208. /* Wait for enable to take effect */
  209. DELAY(1);
  210. /* Initialise lane configuration */
  211. for (phy = 0; phy < FUDW_PHYS; ++phy) {
  212. for (lane = 0; lane < FUDW_LANES_PER_PHY; ++lane)
  213. fupci_phy_write(sc, phy, FUDW_MGMT_PHY_LANE(lane),
  214. FUDW_MGMT_PHY_LANE_INIT);
  215. }
  216. /* Disable the aux clock whilst taking the LTSSM out of reset */
  217. error = clk_disable(sc->pcie_aux_clk);
  218. if (error != 0) {
  219. device_printf(dev, "Cannot disable aux clock: %d\n", error);
  220. return (error);
  221. }
  222. /* Take LTSSM out of reset */
  223. FUDW_MGMT_WRITE(sc, FUDW_MGMT_HOLD_PHY_RST, 0);
  224. /* Enable the aux clock again */
  225. error = clk_enable(sc->pcie_aux_clk);
  226. if (error != 0) {
  227. device_printf(dev, "Cannot re-enable aux clock: %d\n", error);
  228. return (error);
  229. }
  230. /* Put the controller in Root Complex mode */
  231. FUDW_MGMT_WRITE(sc, FUDW_MGMT_DEVICE_TYPE, FUDW_MGMT_DEVICE_TYPE_RC);
  232. /* Hold PERST for 100ms as per the PCIe spec */
  233. DELAY(100000);
  234. /* Deassert PERST_N */
  235. FUDW_MGMT_WRITE(sc, FUDW_MGMT_PERST_N, 1);
  236. return (0);
  237. }
  238. static void
  239. fupci_dbi_protect(struct fupci_softc *sc, bool protect)
  240. {
  241. uint32_t reg;
  242. reg = pci_dw_dbi_rd4(sc->dev, DW_MISC_CONTROL_1);
  243. if (protect)
  244. reg &= ~DBI_RO_WR_EN;
  245. else
  246. reg |= DBI_RO_WR_EN;
  247. pci_dw_dbi_wr4(sc->dev, DW_MISC_CONTROL_1, reg);
  248. }
  249. static int
  250. fupci_init(struct fupci_softc *sc)
  251. {
  252. /* Enable 32-bit I/O window */
  253. fupci_dbi_protect(sc, false);
  254. pci_dw_dbi_wr2(sc->dev, PCIR_IOBASEL_1,
  255. (PCIM_BRIO_32 << 8) | PCIM_BRIO_32);
  256. fupci_dbi_protect(sc, true);
  257. /* Enable LTSSM */
  258. FUDW_MGMT_WRITE(sc, FUDW_MGMT_LTSSM_EN, 1);
  259. return (0);
  260. }
  261. static int
  262. fupci_probe(device_t dev)
  263. {
  264. if (!ofw_bus_status_okay(dev))
  265. return (ENXIO);
  266. if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
  267. return (ENXIO);
  268. device_set_desc(dev, "SiFive FU740 PCIe Controller");
  269. return (BUS_PROBE_DEFAULT);
  270. }
  271. static int
  272. fupci_attach(device_t dev)
  273. {
  274. struct fupci_softc *sc;
  275. phandle_t node;
  276. int error, rid;
  277. sc = device_get_softc(dev);
  278. node = ofw_bus_get_node(dev);
  279. sc->dev = dev;
  280. rid = 0;
  281. error = ofw_bus_find_string_index(node, "reg-names", "dbi", &rid);
  282. if (error != 0) {
  283. device_printf(dev, "Cannot get DBI memory: %d\n", error);
  284. goto fail;
  285. }
  286. sc->dw_sc.dbi_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
  287. RF_ACTIVE);
  288. if (sc->dw_sc.dbi_res == NULL) {
  289. device_printf(dev, "Cannot allocate DBI memory\n");
  290. error = ENXIO;
  291. goto fail;
  292. }
  293. rid = 0;
  294. error = ofw_bus_find_string_index(node, "reg-names", "mgmt", &rid);
  295. if (error != 0) {
  296. device_printf(dev, "Cannot get management space memory: %d\n",
  297. error);
  298. goto fail;
  299. }
  300. sc->mgmt_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
  301. RF_ACTIVE);
  302. if (sc->mgmt_res == NULL) {
  303. device_printf(dev, "Cannot allocate management space memory\n");
  304. error = ENXIO;
  305. goto fail;
  306. }
  307. error = gpio_pin_get_by_ofw_property(dev, node, "reset-gpios",
  308. &sc->porst_pin);
  309. /* Old U-Boot device tree uses perstn-gpios */
  310. if (error == ENOENT)
  311. error = gpio_pin_get_by_ofw_property(dev, node, "perstn-gpios",
  312. &sc->porst_pin);
  313. if (error != 0) {
  314. device_printf(dev, "Cannot get power-on reset GPIO: %d\n",
  315. error);
  316. goto fail;
  317. }
  318. error = gpio_pin_setflags(sc->porst_pin, GPIO_PIN_OUTPUT);
  319. if (error != 0) {
  320. device_printf(dev, "Cannot configure power-on reset GPIO: %d\n",
  321. error);
  322. goto fail;
  323. }
  324. error = gpio_pin_get_by_ofw_property(dev, node, "pwren-gpios",
  325. &sc->pwren_pin);
  326. if (error != 0) {
  327. device_printf(dev, "Cannot get power enable GPIO: %d\n",
  328. error);
  329. goto fail;
  330. }
  331. error = gpio_pin_setflags(sc->pwren_pin, GPIO_PIN_OUTPUT);
  332. if (error != 0) {
  333. device_printf(dev, "Cannot configure power enable GPIO: %d\n",
  334. error);
  335. goto fail;
  336. }
  337. error = clk_get_by_ofw_name(dev, node, "pcie_aux", &sc->pcie_aux_clk);
  338. /* Old U-Boot device tree uses pcieaux */
  339. if (error == ENOENT)
  340. error = clk_get_by_ofw_name(dev, node, "pcieaux",
  341. &sc->pcie_aux_clk);
  342. if (error != 0) {
  343. device_printf(dev, "Cannot get aux clock: %d\n", error);
  344. goto fail;
  345. }
  346. error = hwreset_get_by_ofw_idx(dev, node, 0, &sc->pcie_aux_rst);
  347. if (error != 0) {
  348. device_printf(dev, "Cannot get aux reset: %d\n", error);
  349. goto fail;
  350. }
  351. error = fupci_phy_init(sc);
  352. if (error != 0)
  353. goto fail;
  354. error = pci_dw_init(dev);
  355. if (error != 0)
  356. goto fail;
  357. error = fupci_init(sc);
  358. if (error != 0)
  359. goto fail;
  360. return (bus_generic_attach(dev));
  361. fail:
  362. /* XXX Cleanup */
  363. return (error);
  364. }
  365. static int
  366. fupci_get_link(device_t dev, bool *status)
  367. {
  368. uint32_t reg;
  369. reg = pci_dw_dbi_rd4(dev, FUDW_DBI_PORT_DBG1);
  370. *status = (reg & FUDW_DBI_PORT_DBG1_LINK_UP) != 0 &&
  371. (reg & FUDW_DBI_PORT_DBG1_LINK_IN_TRAINING) == 0;
  372. return (0);
  373. }
  374. static device_method_t fupci_methods[] = {
  375. /* Device interface */
  376. DEVMETHOD(device_probe, fupci_probe),
  377. DEVMETHOD(device_attach, fupci_attach),
  378. /* PCI DW interface */
  379. DEVMETHOD(pci_dw_get_link, fupci_get_link),
  380. DEVMETHOD_END
  381. };
  382. DEFINE_CLASS_1(pcib, fupci_driver, fupci_methods,
  383. sizeof(struct fupci_softc), pci_dw_driver);
  384. DRIVER_MODULE(fu740_pci_dw, simplebus, fupci_driver, NULL, NULL);