pci_mpc85xx.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright 2006-2007 by Juniper Networks.
  5. * Copyright 2008 Semihalf.
  6. * Copyright 2010 The FreeBSD Foundation
  7. * All rights reserved.
  8. *
  9. * Portions of this software were developed by Semihalf
  10. * under sponsorship from the FreeBSD Foundation.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * From: FreeBSD: src/sys/powerpc/mpc85xx/pci_ocp.c,v 1.9 2010/03/23 23:46:28 marcel
  36. */
  37. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD$");
  39. #include <sys/param.h>
  40. #include <sys/systm.h>
  41. #include <sys/ktr.h>
  42. #include <sys/sockio.h>
  43. #include <sys/mbuf.h>
  44. #include <sys/malloc.h>
  45. #include <sys/kernel.h>
  46. #include <sys/module.h>
  47. #include <sys/socket.h>
  48. #include <sys/queue.h>
  49. #include <sys/bus.h>
  50. #include <sys/lock.h>
  51. #include <sys/mutex.h>
  52. #include <sys/queue.h>
  53. #include <sys/rman.h>
  54. #include <sys/endian.h>
  55. #include <sys/vmem.h>
  56. #include <vm/vm.h>
  57. #include <vm/pmap.h>
  58. #include <dev/ofw/ofw_pci.h>
  59. #include <dev/ofw/ofw_bus.h>
  60. #include <dev/ofw/ofw_bus_subr.h>
  61. #include <dev/ofw/ofwpci.h>
  62. #include <dev/pci/pcivar.h>
  63. #include <dev/pci/pcireg.h>
  64. #include <dev/pci/pcib_private.h>
  65. #include "ofw_bus_if.h"
  66. #include "pcib_if.h"
  67. #include "pic_if.h"
  68. #include <machine/resource.h>
  69. #include <machine/bus.h>
  70. #include <machine/intr_machdep.h>
  71. #include <powerpc/mpc85xx/mpc85xx.h>
  72. #define REG_CFG_ADDR 0x0000
  73. #define CONFIG_ACCESS_ENABLE 0x80000000
  74. #define REG_CFG_DATA 0x0004
  75. #define REG_INT_ACK 0x0008
  76. #define REG_PEX_IP_BLK_REV1 0x0bf8
  77. #define IP_MJ_M 0x0000ff00
  78. #define IP_MJ_S 8
  79. #define IP_MN_M 0x000000ff
  80. #define IP_MN_S 0
  81. #define REG_POTAR(n) (0x0c00 + 0x20 * (n))
  82. #define REG_POTEAR(n) (0x0c04 + 0x20 * (n))
  83. #define REG_POWBAR(n) (0x0c08 + 0x20 * (n))
  84. #define REG_POWAR(n) (0x0c10 + 0x20 * (n))
  85. #define REG_PITAR(n) (0x0e00 - 0x20 * (n))
  86. #define REG_PIWBAR(n) (0x0e08 - 0x20 * (n))
  87. #define REG_PIWBEAR(n) (0x0e0c - 0x20 * (n))
  88. #define REG_PIWAR(n) (0x0e10 - 0x20 * (n))
  89. #define PIWAR_EN 0x80000000
  90. #define PIWAR_PF 0x40000000
  91. #define PIWAR_TRGT_M 0x00f00000
  92. #define PIWAR_TRGT_S 20
  93. #define PIWAR_TRGT_CCSR 0xe
  94. #define PIWAR_TRGT_LOCAL 0xf
  95. #define REG_PEX_MES_DR 0x0020
  96. #define REG_PEX_MES_IER 0x0028
  97. #define REG_PEX_ERR_DR 0x0e00
  98. #define REG_PEX_ERR_EN 0x0e08
  99. #define REG_PEX_ERR_DR 0x0e00
  100. #define REG_PEX_ERR_DR_ME 0x80000000
  101. #define REG_PEX_ERR_DR_PCT 0x800000
  102. #define REG_PEX_ERR_DR_PAT 0x400000
  103. #define REG_PEX_ERR_DR_PCAC 0x200000
  104. #define REG_PEX_ERR_DR_PNM 0x100000
  105. #define REG_PEX_ERR_DR_CDNSC 0x80000
  106. #define REG_PEX_ERR_DR_CRSNC 0x40000
  107. #define REG_PEX_ERR_DR_ICCA 0x20000
  108. #define REG_PEX_ERR_DR_IACA 0x10000
  109. #define REG_PEX_ERR_DR_CRST 0x8000
  110. #define REG_PEX_ERR_DR_MIS 0x4000
  111. #define REG_PEX_ERR_DR_IOIS 0x2000
  112. #define REG_PEX_ERR_DR_CIS 0x1000
  113. #define REG_PEX_ERR_DR_CIEP 0x800
  114. #define REG_PEX_ERR_DR_IOIEP 0x400
  115. #define REG_PEX_ERR_DR_OAC 0x200
  116. #define REG_PEX_ERR_DR_IOIA 0x100
  117. #define REG_PEX_ERR_DR_IMBA 0x80
  118. #define REG_PEX_ERR_DR_IIOBA 0x40
  119. #define REG_PEX_ERR_DR_LDDE 0x20
  120. #define REG_PEX_ERR_EN 0x0e08
  121. #define PCIR_LTSSM 0x404
  122. #define LTSSM_STAT_L0 0x16
  123. #define DEVFN(b, s, f) ((b << 16) | (s << 8) | f)
  124. #define FSL_NUM_MSIS 256 /* 8 registers of 32 bits (8 hardware IRQs) */
  125. struct fsl_pcib_softc {
  126. struct ofw_pci_softc pci_sc;
  127. device_t sc_dev;
  128. struct mtx sc_cfg_mtx;
  129. int sc_ip_maj;
  130. int sc_ip_min;
  131. int sc_iomem_target;
  132. bus_addr_t sc_iomem_start, sc_iomem_end;
  133. int sc_ioport_target;
  134. bus_addr_t sc_ioport_start, sc_ioport_end;
  135. struct resource *sc_res;
  136. bus_space_handle_t sc_bsh;
  137. bus_space_tag_t sc_bst;
  138. int sc_rid;
  139. struct resource *sc_irq_res;
  140. void *sc_ih;
  141. int sc_busnr;
  142. int sc_pcie;
  143. uint8_t sc_pcie_capreg; /* PCI-E Capability Reg Set */
  144. };
  145. struct fsl_pcib_err_dr {
  146. const char *msg;
  147. uint32_t err_dr_mask;
  148. };
  149. struct fsl_msi_map {
  150. SLIST_ENTRY(fsl_msi_map) slist;
  151. uint32_t irq_base;
  152. bus_addr_t target;
  153. };
  154. SLIST_HEAD(msi_head, fsl_msi_map) fsl_msis = SLIST_HEAD_INITIALIZER(msi_head);
  155. static const struct fsl_pcib_err_dr pci_err[] = {
  156. {"ME", REG_PEX_ERR_DR_ME},
  157. {"PCT", REG_PEX_ERR_DR_PCT},
  158. {"PAT", REG_PEX_ERR_DR_PAT},
  159. {"PCAC", REG_PEX_ERR_DR_PCAC},
  160. {"PNM", REG_PEX_ERR_DR_PNM},
  161. {"CDNSC", REG_PEX_ERR_DR_CDNSC},
  162. {"CRSNC", REG_PEX_ERR_DR_CRSNC},
  163. {"ICCA", REG_PEX_ERR_DR_ICCA},
  164. {"IACA", REG_PEX_ERR_DR_IACA},
  165. {"CRST", REG_PEX_ERR_DR_CRST},
  166. {"MIS", REG_PEX_ERR_DR_MIS},
  167. {"IOIS", REG_PEX_ERR_DR_IOIS},
  168. {"CIS", REG_PEX_ERR_DR_CIS},
  169. {"CIEP", REG_PEX_ERR_DR_CIEP},
  170. {"IOIEP", REG_PEX_ERR_DR_IOIEP},
  171. {"OAC", REG_PEX_ERR_DR_OAC},
  172. {"IOIA", REG_PEX_ERR_DR_IOIA},
  173. {"IMBA", REG_PEX_ERR_DR_IMBA},
  174. {"IIOBA", REG_PEX_ERR_DR_IIOBA},
  175. {"LDDE", REG_PEX_ERR_DR_LDDE}
  176. };
  177. /* Local forward declerations. */
  178. static uint32_t fsl_pcib_cfgread(struct fsl_pcib_softc *, u_int, u_int, u_int,
  179. u_int, int);
  180. static void fsl_pcib_cfgwrite(struct fsl_pcib_softc *, u_int, u_int, u_int,
  181. u_int, uint32_t, int);
  182. static int fsl_pcib_decode_win(phandle_t, struct fsl_pcib_softc *);
  183. static void fsl_pcib_err_init(device_t);
  184. static void fsl_pcib_inbound(struct fsl_pcib_softc *, int, int, uint64_t,
  185. uint64_t, uint64_t);
  186. static void fsl_pcib_outbound(struct fsl_pcib_softc *, int, int, uint64_t,
  187. uint64_t, uint64_t);
  188. /* Forward declerations. */
  189. static int fsl_pcib_attach(device_t);
  190. static int fsl_pcib_detach(device_t);
  191. static int fsl_pcib_probe(device_t);
  192. static int fsl_pcib_maxslots(device_t);
  193. static uint32_t fsl_pcib_read_config(device_t, u_int, u_int, u_int, u_int, int);
  194. static void fsl_pcib_write_config(device_t, u_int, u_int, u_int, u_int,
  195. uint32_t, int);
  196. static int fsl_pcib_alloc_msi(device_t dev, device_t child,
  197. int count, int maxcount, int *irqs);
  198. static int fsl_pcib_release_msi(device_t dev, device_t child,
  199. int count, int *irqs);
  200. static int fsl_pcib_alloc_msix(device_t dev, device_t child, int *irq);
  201. static int fsl_pcib_release_msix(device_t dev, device_t child, int irq);
  202. static int fsl_pcib_map_msi(device_t dev, device_t child,
  203. int irq, uint64_t *addr, uint32_t *data);
  204. static vmem_t *msi_vmem; /* Global MSI vmem, holds all MSI ranges. */
  205. /*
  206. * Bus interface definitions.
  207. */
  208. static device_method_t fsl_pcib_methods[] = {
  209. /* Device interface */
  210. DEVMETHOD(device_probe, fsl_pcib_probe),
  211. DEVMETHOD(device_attach, fsl_pcib_attach),
  212. DEVMETHOD(device_detach, fsl_pcib_detach),
  213. /* pcib interface */
  214. DEVMETHOD(pcib_maxslots, fsl_pcib_maxslots),
  215. DEVMETHOD(pcib_read_config, fsl_pcib_read_config),
  216. DEVMETHOD(pcib_write_config, fsl_pcib_write_config),
  217. DEVMETHOD(pcib_alloc_msi, fsl_pcib_alloc_msi),
  218. DEVMETHOD(pcib_release_msi, fsl_pcib_release_msi),
  219. DEVMETHOD(pcib_alloc_msix, fsl_pcib_alloc_msix),
  220. DEVMETHOD(pcib_release_msix, fsl_pcib_release_msix),
  221. DEVMETHOD(pcib_map_msi, fsl_pcib_map_msi),
  222. DEVMETHOD_END
  223. };
  224. static devclass_t fsl_pcib_devclass;
  225. DEFINE_CLASS_1(pcib, fsl_pcib_driver, fsl_pcib_methods,
  226. sizeof(struct fsl_pcib_softc), ofw_pci_driver);
  227. EARLY_DRIVER_MODULE(pcib, ofwbus, fsl_pcib_driver, fsl_pcib_devclass, 0, 0,
  228. BUS_PASS_BUS);
  229. static void
  230. fsl_pcib_err_intr(void *v)
  231. {
  232. struct fsl_pcib_softc *sc;
  233. device_t dev;
  234. uint32_t err_reg, clear_reg;
  235. uint8_t i;
  236. dev = (device_t)v;
  237. sc = device_get_softc(dev);
  238. clear_reg = 0;
  239. err_reg = bus_space_read_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR);
  240. /* Check which one error occurred */
  241. for (i = 0; i < sizeof(pci_err)/sizeof(struct fsl_pcib_err_dr); i++) {
  242. if (err_reg & pci_err[i].err_dr_mask) {
  243. device_printf(dev, "PCI %d: report %s error\n",
  244. device_get_unit(dev), pci_err[i].msg);
  245. clear_reg |= pci_err[i].err_dr_mask;
  246. }
  247. }
  248. /* Clear pending errors */
  249. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR, clear_reg);
  250. }
  251. static int
  252. fsl_pcib_probe(device_t dev)
  253. {
  254. if (ofw_bus_get_type(dev) == NULL ||
  255. strcmp(ofw_bus_get_type(dev), "pci") != 0)
  256. return (ENXIO);
  257. if (!(ofw_bus_is_compatible(dev, "fsl,mpc8540-pci") ||
  258. ofw_bus_is_compatible(dev, "fsl,mpc8540-pcie") ||
  259. ofw_bus_is_compatible(dev, "fsl,mpc8548-pcie") ||
  260. ofw_bus_is_compatible(dev, "fsl,p5020-pcie") ||
  261. ofw_bus_is_compatible(dev, "fsl,qoriq-pcie-v2.2") ||
  262. ofw_bus_is_compatible(dev, "fsl,qoriq-pcie")))
  263. return (ENXIO);
  264. device_set_desc(dev, "Freescale Integrated PCI/PCI-E Controller");
  265. return (BUS_PROBE_DEFAULT);
  266. }
  267. static int
  268. fsl_pcib_attach(device_t dev)
  269. {
  270. struct fsl_pcib_softc *sc;
  271. phandle_t node;
  272. uint32_t cfgreg, brctl, ipreg;
  273. int error, rid;
  274. uint8_t ltssm, capptr;
  275. sc = device_get_softc(dev);
  276. sc->sc_dev = dev;
  277. sc->sc_rid = 0;
  278. sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
  279. RF_ACTIVE);
  280. if (sc->sc_res == NULL) {
  281. device_printf(dev, "could not map I/O memory\n");
  282. return (ENXIO);
  283. }
  284. sc->sc_bst = rman_get_bustag(sc->sc_res);
  285. sc->sc_bsh = rman_get_bushandle(sc->sc_res);
  286. sc->sc_busnr = 0;
  287. ipreg = bus_read_4(sc->sc_res, REG_PEX_IP_BLK_REV1);
  288. sc->sc_ip_min = (ipreg & IP_MN_M) >> IP_MN_S;
  289. sc->sc_ip_maj = (ipreg & IP_MJ_M) >> IP_MJ_S;
  290. mtx_init(&sc->sc_cfg_mtx, "pcicfg", NULL, MTX_SPIN);
  291. cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_VENDOR, 2);
  292. if (cfgreg != 0x1057 && cfgreg != 0x1957)
  293. goto err;
  294. capptr = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_CAP_PTR, 1);
  295. while (capptr != 0) {
  296. cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, capptr, 2);
  297. switch (cfgreg & 0xff) {
  298. case PCIY_PCIX:
  299. break;
  300. case PCIY_EXPRESS:
  301. sc->sc_pcie = 1;
  302. sc->sc_pcie_capreg = capptr;
  303. break;
  304. }
  305. capptr = (cfgreg >> 8) & 0xff;
  306. }
  307. node = ofw_bus_get_node(dev);
  308. /*
  309. * Initialize generic OF PCI interface (ranges, etc.)
  310. */
  311. error = ofw_pci_init(dev);
  312. if (error)
  313. return (error);
  314. /*
  315. * Configure decode windows for PCI(E) access.
  316. */
  317. if (fsl_pcib_decode_win(node, sc) != 0)
  318. goto err;
  319. cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_COMMAND, 2);
  320. cfgreg |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
  321. PCIM_CMD_PORTEN;
  322. fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_COMMAND, cfgreg, 2);
  323. /* Reset the bus. Needed for Radeon video cards. */
  324. brctl = fsl_pcib_read_config(sc->sc_dev, 0, 0, 0,
  325. PCIR_BRIDGECTL_1, 1);
  326. brctl |= PCIB_BCR_SECBUS_RESET;
  327. fsl_pcib_write_config(sc->sc_dev, 0, 0, 0,
  328. PCIR_BRIDGECTL_1, brctl, 1);
  329. DELAY(100000);
  330. brctl &= ~PCIB_BCR_SECBUS_RESET;
  331. fsl_pcib_write_config(sc->sc_dev, 0, 0, 0,
  332. PCIR_BRIDGECTL_1, brctl, 1);
  333. DELAY(100000);
  334. if (sc->sc_pcie) {
  335. ltssm = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_LTSSM, 1);
  336. if (ltssm < LTSSM_STAT_L0) {
  337. if (bootverbose)
  338. printf("PCI %d: no PCIE link, skipping\n",
  339. device_get_unit(dev));
  340. return (0);
  341. }
  342. }
  343. /* Allocate irq */
  344. rid = 0;
  345. sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
  346. RF_ACTIVE | RF_SHAREABLE);
  347. if (sc->sc_irq_res == NULL) {
  348. error = fsl_pcib_detach(dev);
  349. if (error != 0) {
  350. device_printf(dev,
  351. "Detach of the driver failed with error %d\n",
  352. error);
  353. }
  354. return (ENXIO);
  355. }
  356. /* Setup interrupt handler */
  357. error = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
  358. NULL, fsl_pcib_err_intr, dev, &sc->sc_ih);
  359. if (error != 0) {
  360. device_printf(dev, "Could not setup irq, %d\n", error);
  361. sc->sc_ih = NULL;
  362. error = fsl_pcib_detach(dev);
  363. if (error != 0) {
  364. device_printf(dev,
  365. "Detach of the driver failed with error %d\n",
  366. error);
  367. }
  368. return (ENXIO);
  369. }
  370. fsl_pcib_err_init(dev);
  371. return (ofw_pci_attach(dev));
  372. err:
  373. return (ENXIO);
  374. }
  375. static uint32_t
  376. fsl_pcib_cfgread(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
  377. u_int reg, int bytes)
  378. {
  379. uint32_t addr, data;
  380. addr = CONFIG_ACCESS_ENABLE;
  381. addr |= (bus & 0xff) << 16;
  382. addr |= (slot & 0x1f) << 11;
  383. addr |= (func & 0x7) << 8;
  384. addr |= reg & 0xfc;
  385. if (sc->sc_pcie)
  386. addr |= (reg & 0xf00) << 16;
  387. mtx_lock_spin(&sc->sc_cfg_mtx);
  388. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
  389. switch (bytes) {
  390. case 1:
  391. data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
  392. REG_CFG_DATA + (reg & 3));
  393. break;
  394. case 2:
  395. data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
  396. REG_CFG_DATA + (reg & 2)));
  397. break;
  398. case 4:
  399. data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
  400. REG_CFG_DATA));
  401. break;
  402. default:
  403. data = ~0;
  404. break;
  405. }
  406. mtx_unlock_spin(&sc->sc_cfg_mtx);
  407. return (data);
  408. }
  409. static void
  410. fsl_pcib_cfgwrite(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
  411. u_int reg, uint32_t data, int bytes)
  412. {
  413. uint32_t addr;
  414. addr = CONFIG_ACCESS_ENABLE;
  415. addr |= (bus & 0xff) << 16;
  416. addr |= (slot & 0x1f) << 11;
  417. addr |= (func & 0x7) << 8;
  418. addr |= reg & 0xfc;
  419. if (sc->sc_pcie)
  420. addr |= (reg & 0xf00) << 16;
  421. mtx_lock_spin(&sc->sc_cfg_mtx);
  422. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
  423. switch (bytes) {
  424. case 1:
  425. bus_space_write_1(sc->sc_bst, sc->sc_bsh,
  426. REG_CFG_DATA + (reg & 3), data);
  427. break;
  428. case 2:
  429. bus_space_write_2(sc->sc_bst, sc->sc_bsh,
  430. REG_CFG_DATA + (reg & 2), htole16(data));
  431. break;
  432. case 4:
  433. bus_space_write_4(sc->sc_bst, sc->sc_bsh,
  434. REG_CFG_DATA, htole32(data));
  435. break;
  436. }
  437. mtx_unlock_spin(&sc->sc_cfg_mtx);
  438. }
  439. #if 0
  440. static void
  441. dump(struct fsl_pcib_softc *sc)
  442. {
  443. unsigned int i;
  444. #define RD(o) bus_space_read_4(sc->sc_bst, sc->sc_bsh, o)
  445. for (i = 0; i < 5; i++) {
  446. printf("POTAR%u =0x%08x\n", i, RD(REG_POTAR(i)));
  447. printf("POTEAR%u =0x%08x\n", i, RD(REG_POTEAR(i)));
  448. printf("POWBAR%u =0x%08x\n", i, RD(REG_POWBAR(i)));
  449. printf("POWAR%u =0x%08x\n", i, RD(REG_POWAR(i)));
  450. }
  451. printf("\n");
  452. for (i = 1; i < 4; i++) {
  453. printf("PITAR%u =0x%08x\n", i, RD(REG_PITAR(i)));
  454. printf("PIWBAR%u =0x%08x\n", i, RD(REG_PIWBAR(i)));
  455. printf("PIWBEAR%u=0x%08x\n", i, RD(REG_PIWBEAR(i)));
  456. printf("PIWAR%u =0x%08x\n", i, RD(REG_PIWAR(i)));
  457. }
  458. printf("\n");
  459. #undef RD
  460. for (i = 0; i < 0x48; i += 4) {
  461. printf("cfg%02x=0x%08x\n", i, fsl_pcib_cfgread(sc, 0, 0, 0,
  462. i, 4));
  463. }
  464. }
  465. #endif
  466. static int
  467. fsl_pcib_maxslots(device_t dev)
  468. {
  469. struct fsl_pcib_softc *sc = device_get_softc(dev);
  470. return ((sc->sc_pcie) ? 0 : PCI_SLOTMAX);
  471. }
  472. static uint32_t
  473. fsl_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
  474. u_int reg, int bytes)
  475. {
  476. struct fsl_pcib_softc *sc = device_get_softc(dev);
  477. u_int devfn;
  478. if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
  479. return (~0);
  480. devfn = DEVFN(bus, slot, func);
  481. return (fsl_pcib_cfgread(sc, bus, slot, func, reg, bytes));
  482. }
  483. static void
  484. fsl_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
  485. u_int reg, uint32_t val, int bytes)
  486. {
  487. struct fsl_pcib_softc *sc = device_get_softc(dev);
  488. if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
  489. return;
  490. fsl_pcib_cfgwrite(sc, bus, slot, func, reg, val, bytes);
  491. }
  492. static void
  493. fsl_pcib_inbound(struct fsl_pcib_softc *sc, int wnd, int tgt, uint64_t start,
  494. uint64_t size, uint64_t pci_start)
  495. {
  496. uint32_t attr, bar, tar;
  497. KASSERT(wnd > 0, ("%s: inbound window 0 is invalid", __func__));
  498. attr = PIWAR_EN;
  499. switch (tgt) {
  500. case -1:
  501. attr &= ~PIWAR_EN;
  502. break;
  503. case PIWAR_TRGT_LOCAL:
  504. attr |= (ffsl(size) - 2);
  505. default:
  506. attr |= (tgt << PIWAR_TRGT_S);
  507. break;
  508. }
  509. tar = start >> 12;
  510. bar = pci_start >> 12;
  511. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PITAR(wnd), tar);
  512. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBEAR(wnd), 0);
  513. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBAR(wnd), bar);
  514. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWAR(wnd), attr);
  515. }
  516. static void
  517. fsl_pcib_outbound(struct fsl_pcib_softc *sc, int wnd, int res, uint64_t start,
  518. uint64_t size, uint64_t pci_start)
  519. {
  520. uint32_t attr, bar, tar;
  521. switch (res) {
  522. case SYS_RES_MEMORY:
  523. attr = 0x80044000 | (ffsll(size) - 2);
  524. break;
  525. case SYS_RES_IOPORT:
  526. attr = 0x80088000 | (ffsll(size) - 2);
  527. break;
  528. default:
  529. attr = 0x0004401f;
  530. break;
  531. }
  532. bar = start >> 12;
  533. tar = pci_start >> 12;
  534. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTAR(wnd), tar);
  535. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTEAR(wnd), 0);
  536. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWBAR(wnd), bar);
  537. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWAR(wnd), attr);
  538. }
  539. static void
  540. fsl_pcib_err_init(device_t dev)
  541. {
  542. struct fsl_pcib_softc *sc;
  543. uint16_t sec_stat, dsr;
  544. uint32_t dcr, err_en;
  545. sc = device_get_softc(dev);
  546. sec_stat = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_SECSTAT_1, 2);
  547. if (sec_stat)
  548. fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_SECSTAT_1, 0xffff, 2);
  549. if (sc->sc_pcie) {
  550. /* Clear error bits */
  551. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_IER,
  552. 0xffffffff);
  553. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_DR,
  554. 0xffffffff);
  555. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR,
  556. 0xffffffff);
  557. dsr = fsl_pcib_cfgread(sc, 0, 0, 0,
  558. sc->sc_pcie_capreg + PCIER_DEVICE_STA, 2);
  559. if (dsr)
  560. fsl_pcib_cfgwrite(sc, 0, 0, 0,
  561. sc->sc_pcie_capreg + PCIER_DEVICE_STA,
  562. 0xffff, 2);
  563. /* Enable all errors reporting */
  564. err_en = 0x00bfff00;
  565. bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_EN,
  566. err_en);
  567. /* Enable error reporting: URR, FER, NFER */
  568. dcr = fsl_pcib_cfgread(sc, 0, 0, 0,
  569. sc->sc_pcie_capreg + PCIER_DEVICE_CTL, 4);
  570. dcr |= PCIEM_CTL_URR_ENABLE | PCIEM_CTL_FER_ENABLE |
  571. PCIEM_CTL_NFER_ENABLE;
  572. fsl_pcib_cfgwrite(sc, 0, 0, 0,
  573. sc->sc_pcie_capreg + PCIER_DEVICE_CTL, dcr, 4);
  574. }
  575. }
  576. static int
  577. fsl_pcib_detach(device_t dev)
  578. {
  579. struct fsl_pcib_softc *sc;
  580. sc = device_get_softc(dev);
  581. mtx_destroy(&sc->sc_cfg_mtx);
  582. return (bus_generic_detach(dev));
  583. }
  584. static int
  585. fsl_pcib_decode_win(phandle_t node, struct fsl_pcib_softc *sc)
  586. {
  587. device_t dev;
  588. int error, i, trgt;
  589. dev = sc->sc_dev;
  590. fsl_pcib_outbound(sc, 0, -1, 0, 0, 0);
  591. /*
  592. * Configure LAW decode windows.
  593. */
  594. error = law_pci_target(sc->sc_res, &sc->sc_iomem_target,
  595. &sc->sc_ioport_target);
  596. if (error != 0) {
  597. device_printf(dev, "could not retrieve PCI LAW target info\n");
  598. return (error);
  599. }
  600. for (i = 0; i < sc->pci_sc.sc_nrange; i++) {
  601. switch (sc->pci_sc.sc_range[i].pci_hi &
  602. OFW_PCI_PHYS_HI_SPACEMASK) {
  603. case OFW_PCI_PHYS_HI_SPACE_CONFIG:
  604. continue;
  605. case OFW_PCI_PHYS_HI_SPACE_IO:
  606. trgt = sc->sc_ioport_target;
  607. fsl_pcib_outbound(sc, 2, SYS_RES_IOPORT,
  608. sc->pci_sc.sc_range[i].host,
  609. sc->pci_sc.sc_range[i].size,
  610. sc->pci_sc.sc_range[i].pci);
  611. sc->sc_ioport_start = sc->pci_sc.sc_range[i].pci;
  612. sc->sc_ioport_end = sc->pci_sc.sc_range[i].pci +
  613. sc->pci_sc.sc_range[i].size - 1;
  614. break;
  615. case OFW_PCI_PHYS_HI_SPACE_MEM32:
  616. case OFW_PCI_PHYS_HI_SPACE_MEM64:
  617. trgt = sc->sc_iomem_target;
  618. fsl_pcib_outbound(sc, 1, SYS_RES_MEMORY,
  619. sc->pci_sc.sc_range[i].host,
  620. sc->pci_sc.sc_range[i].size,
  621. sc->pci_sc.sc_range[i].pci);
  622. sc->sc_iomem_start = sc->pci_sc.sc_range[i].pci;
  623. sc->sc_iomem_end = sc->pci_sc.sc_range[i].pci +
  624. sc->pci_sc.sc_range[i].size - 1;
  625. break;
  626. default:
  627. panic("Unknown range type %#x\n",
  628. sc->pci_sc.sc_range[i].pci_hi &
  629. OFW_PCI_PHYS_HI_SPACEMASK);
  630. }
  631. error = law_enable(trgt, sc->pci_sc.sc_range[i].host,
  632. sc->pci_sc.sc_range[i].size);
  633. if (error != 0) {
  634. device_printf(dev, "could not program LAW for range "
  635. "%d\n", i);
  636. return (error);
  637. }
  638. }
  639. /*
  640. * Set outbout and inbound windows.
  641. */
  642. fsl_pcib_outbound(sc, 3, -1, 0, 0, 0);
  643. fsl_pcib_outbound(sc, 4, -1, 0, 0, 0);
  644. fsl_pcib_inbound(sc, 1, -1, 0, 0, 0);
  645. fsl_pcib_inbound(sc, 2, -1, 0, 0, 0);
  646. fsl_pcib_inbound(sc, 3, PIWAR_TRGT_LOCAL, 0,
  647. ptoa(Maxmem), 0);
  648. /* Direct-map the CCSR for MSIs. */
  649. /* Freescale PCIe 2.x has a dedicated MSI window. */
  650. /* inbound window 8 makes it hit 0xD00 offset, the MSI window. */
  651. if (sc->sc_ip_maj >= 2)
  652. fsl_pcib_inbound(sc, 8, PIWAR_TRGT_CCSR, ccsrbar_pa,
  653. ccsrbar_size, ccsrbar_pa);
  654. else
  655. fsl_pcib_inbound(sc, 1, PIWAR_TRGT_CCSR, ccsrbar_pa,
  656. ccsrbar_size, ccsrbar_pa);
  657. return (0);
  658. }
  659. static int fsl_pcib_alloc_msi(device_t dev, device_t child,
  660. int count, int maxcount, int *irqs)
  661. {
  662. struct fsl_pcib_softc *sc;
  663. vmem_addr_t start;
  664. int err, i;
  665. sc = device_get_softc(dev);
  666. if (msi_vmem == NULL)
  667. return (ENODEV);
  668. err = vmem_xalloc(msi_vmem, count, powerof2(count), 0, 0,
  669. VMEM_ADDR_MIN, VMEM_ADDR_MAX, M_BESTFIT | M_WAITOK, &start);
  670. if (err)
  671. return (err);
  672. for (i = 0; i < count; i++)
  673. irqs[i] = start + i;
  674. return (0);
  675. }
  676. static int fsl_pcib_release_msi(device_t dev, device_t child,
  677. int count, int *irqs)
  678. {
  679. if (msi_vmem == NULL)
  680. return (ENODEV);
  681. vmem_xfree(msi_vmem, irqs[0], count);
  682. return (0);
  683. }
  684. static int fsl_pcib_alloc_msix(device_t dev, device_t child, int *irq)
  685. {
  686. return (fsl_pcib_alloc_msi(dev, child, 1, 1, irq));
  687. }
  688. static int fsl_pcib_release_msix(device_t dev, device_t child, int irq)
  689. {
  690. return (fsl_pcib_release_msi(dev, child, 1, &irq));
  691. }
  692. static int fsl_pcib_map_msi(device_t dev, device_t child,
  693. int irq, uint64_t *addr, uint32_t *data)
  694. {
  695. struct fsl_msi_map *mp;
  696. SLIST_FOREACH(mp, &fsl_msis, slist) {
  697. if (irq >= mp->irq_base && irq < mp->irq_base + FSL_NUM_MSIS)
  698. break;
  699. }
  700. if (mp == NULL)
  701. return (ENODEV);
  702. *data = (irq & 255);
  703. *addr = ccsrbar_pa + mp->target;
  704. return (0);
  705. }
  706. /*
  707. * Linux device trees put the msi@<x> as children of the SoC, with ranges based
  708. * on the CCSR. Since rman doesn't permit overlapping or sub-ranges between
  709. * devices (bus_space_subregion(9) could do it, but let's not touch the PIC
  710. * driver just to allocate a subregion for a sibling driver). This driver will
  711. * use ccsr_write() and ccsr_read() instead.
  712. */
  713. #define FSL_NUM_IRQS 8
  714. #define FSL_NUM_MSI_PER_IRQ 32
  715. #define FSL_MSI_TARGET 0x140
  716. struct fsl_msi_softc {
  717. vm_offset_t sc_base;
  718. vm_offset_t sc_target;
  719. int sc_msi_base_irq;
  720. struct fsl_msi_map sc_map;
  721. struct fsl_msi_irq {
  722. /* This struct gets passed as the filter private data. */
  723. struct fsl_msi_softc *sc_ptr; /* Pointer back to softc. */
  724. struct resource *res;
  725. int irq;
  726. void *cookie;
  727. int vectors[FSL_NUM_MSI_PER_IRQ];
  728. vm_offset_t reg;
  729. } sc_msi_irq[FSL_NUM_IRQS];
  730. };
  731. static int
  732. fsl_msi_intr_filter(void *priv)
  733. {
  734. struct fsl_msi_irq *data = priv;
  735. uint32_t reg;
  736. int i;
  737. reg = ccsr_read4(ccsrbar_va + data->reg);
  738. i = 0;
  739. while (reg != 0) {
  740. if (reg & 1)
  741. powerpc_dispatch_intr(data->vectors[i], NULL);
  742. reg >>= 1;
  743. i++;
  744. }
  745. return (FILTER_HANDLED);
  746. }
  747. static int
  748. fsl_msi_probe(device_t dev)
  749. {
  750. if (!ofw_bus_is_compatible(dev, "fsl,mpic-msi"))
  751. return (ENXIO);
  752. device_set_desc(dev, "Freescale MSI");
  753. return (BUS_PROBE_DEFAULT);
  754. }
  755. static int
  756. fsl_msi_attach(device_t dev)
  757. {
  758. struct fsl_msi_softc *sc;
  759. struct fsl_msi_irq *irq;
  760. int i;
  761. sc = device_get_softc(dev);
  762. if (msi_vmem == NULL)
  763. msi_vmem = vmem_create("MPIC MSI", 0, 0, 1, 0, M_BESTFIT | M_WAITOK);
  764. /* Manually play with resource entries. */
  765. sc->sc_base = bus_get_resource_start(dev, SYS_RES_MEMORY, 0);
  766. sc->sc_map.target = bus_get_resource_start(dev, SYS_RES_MEMORY, 1);
  767. if (sc->sc_map.target == 0)
  768. sc->sc_map.target = sc->sc_base + FSL_MSI_TARGET;
  769. for (i = 0; i < FSL_NUM_IRQS; i++) {
  770. irq = &sc->sc_msi_irq[i];
  771. irq->irq = i;
  772. irq->reg = sc->sc_base + 16 * i;
  773. irq->res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
  774. &irq->irq, RF_ACTIVE);
  775. bus_setup_intr(dev, irq->res, INTR_TYPE_MISC | INTR_MPSAFE,
  776. fsl_msi_intr_filter, NULL, irq, &irq->cookie);
  777. }
  778. sc->sc_map.irq_base = powerpc_register_pic(dev, ofw_bus_get_node(dev),
  779. FSL_NUM_MSIS, 0, 0);
  780. /* Let vmem and the IRQ subsystem work their magic for allocations. */
  781. vmem_add(msi_vmem, sc->sc_map.irq_base, FSL_NUM_MSIS, M_WAITOK);
  782. SLIST_INSERT_HEAD(&fsl_msis, &sc->sc_map, slist);
  783. return (0);
  784. }
  785. static void
  786. fsl_msi_enable(device_t dev, u_int irq, u_int vector, void **priv)
  787. {
  788. struct fsl_msi_softc *sc;
  789. struct fsl_msi_irq *irqd;
  790. sc = device_get_softc(dev);
  791. irqd = &sc->sc_msi_irq[irq / FSL_NUM_MSI_PER_IRQ];
  792. irqd->vectors[irq % FSL_NUM_MSI_PER_IRQ] = vector;
  793. }
  794. static device_method_t fsl_msi_methods[] = {
  795. DEVMETHOD(device_probe, fsl_msi_probe),
  796. DEVMETHOD(device_attach, fsl_msi_attach),
  797. DEVMETHOD(pic_enable, fsl_msi_enable),
  798. DEVMETHOD_END
  799. };
  800. static devclass_t fsl_msi_devclass;
  801. static driver_t fsl_msi_driver = {
  802. "fsl_msi",
  803. fsl_msi_methods,
  804. sizeof(struct fsl_msi_softc)
  805. };
  806. EARLY_DRIVER_MODULE(fsl_msi, simplebus, fsl_msi_driver, fsl_msi_devclass, 0, 0,
  807. BUS_PASS_INTERRUPT + 1);