PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/sys/dev/puc/puc.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 766 lines | 600 code | 103 blank | 63 comment | 233 complexity | dd3e91114a0974782e0a3da35bce9ca1 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*-
  2. * Copyright (c) 2006 Marcel Moolenaar
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  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 ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/cdefs.h>
  27. __FBSDID("$FreeBSD$");
  28. #include <sys/param.h>
  29. #include <sys/systm.h>
  30. #include <sys/kernel.h>
  31. #include <sys/bus.h>
  32. #include <sys/conf.h>
  33. #include <sys/malloc.h>
  34. #include <sys/mutex.h>
  35. #include <machine/bus.h>
  36. #include <machine/resource.h>
  37. #include <sys/rman.h>
  38. #include <dev/pci/pcireg.h>
  39. #include <dev/pci/pcivar.h>
  40. #include <dev/puc/puc_bus.h>
  41. #include <dev/puc/puc_cfg.h>
  42. #include <dev/puc/puc_bfe.h>
  43. #define PUC_ISRCCNT 5
  44. struct puc_port {
  45. struct puc_bar *p_bar;
  46. struct resource *p_rres;
  47. struct resource *p_ires;
  48. device_t p_dev;
  49. int p_nr;
  50. int p_type;
  51. int p_rclk;
  52. int p_hasintr:1;
  53. serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
  54. void *p_iharg;
  55. int p_ipend;
  56. };
  57. devclass_t puc_devclass;
  58. const char puc_driver_name[] = "puc";
  59. static MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
  60. struct puc_bar *
  61. puc_get_bar(struct puc_softc *sc, int rid)
  62. {
  63. struct puc_bar *bar;
  64. struct rman *rm;
  65. u_long end, start;
  66. int error, i;
  67. /* Find the BAR entry with the given RID. */
  68. i = 0;
  69. while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
  70. i++;
  71. if (i < PUC_PCI_BARS)
  72. return (&sc->sc_bar[i]);
  73. /* Not found. If we're looking for an unused entry, return NULL. */
  74. if (rid == -1)
  75. return (NULL);
  76. /* Get an unused entry for us to fill. */
  77. bar = puc_get_bar(sc, -1);
  78. if (bar == NULL)
  79. return (NULL);
  80. bar->b_rid = rid;
  81. bar->b_type = SYS_RES_IOPORT;
  82. bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
  83. &bar->b_rid, RF_ACTIVE);
  84. if (bar->b_res == NULL) {
  85. bar->b_rid = rid;
  86. bar->b_type = SYS_RES_MEMORY;
  87. bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
  88. &bar->b_rid, RF_ACTIVE);
  89. if (bar->b_res == NULL) {
  90. bar->b_rid = -1;
  91. return (NULL);
  92. }
  93. }
  94. /* Update our managed space. */
  95. rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
  96. start = rman_get_start(bar->b_res);
  97. end = rman_get_end(bar->b_res);
  98. error = rman_manage_region(rm, start, end);
  99. if (error) {
  100. bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
  101. bar->b_res);
  102. bar->b_res = NULL;
  103. bar->b_rid = -1;
  104. bar = NULL;
  105. }
  106. return (bar);
  107. }
  108. static int
  109. puc_intr(void *arg)
  110. {
  111. struct puc_port *port;
  112. struct puc_softc *sc = arg;
  113. u_long ds, dev, devs;
  114. int i, idx, ipend, isrc, nints;
  115. uint8_t ilr;
  116. nints = 0;
  117. while (1) {
  118. /*
  119. * Obtain the set of devices with pending interrupts.
  120. */
  121. devs = sc->sc_serdevs;
  122. if (sc->sc_ilr == PUC_ILR_DIGI) {
  123. idx = 0;
  124. while (devs & (0xfful << idx)) {
  125. ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
  126. devs &= ~0ul ^ ((u_long)ilr << idx);
  127. idx += 8;
  128. }
  129. } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
  130. /*
  131. * Don't trust the value if it's the same as the option
  132. * register. It may mean that the ILR is not active and
  133. * we're reading the option register instead. This may
  134. * lead to false positives on 8-port boards.
  135. */
  136. ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
  137. if (ilr != (sc->sc_cfg_data & 0xff))
  138. devs &= (u_long)ilr;
  139. }
  140. if (devs == 0UL)
  141. break;
  142. /*
  143. * Obtain the set of interrupt sources from those devices
  144. * that have pending interrupts.
  145. */
  146. ipend = 0;
  147. idx = 0, dev = 1UL;
  148. ds = devs;
  149. while (ds != 0UL) {
  150. while ((ds & dev) == 0UL)
  151. idx++, dev <<= 1;
  152. ds &= ~dev;
  153. port = &sc->sc_port[idx];
  154. port->p_ipend = SERDEV_IPEND(port->p_dev);
  155. ipend |= port->p_ipend;
  156. }
  157. if (ipend == 0)
  158. break;
  159. i = 0, isrc = SER_INT_OVERRUN;
  160. while (ipend) {
  161. while (i < PUC_ISRCCNT && !(ipend & isrc))
  162. i++, isrc <<= 1;
  163. KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
  164. ipend &= ~isrc;
  165. idx = 0, dev = 1UL;
  166. ds = devs;
  167. while (ds != 0UL) {
  168. while ((ds & dev) == 0UL)
  169. idx++, dev <<= 1;
  170. ds &= ~dev;
  171. port = &sc->sc_port[idx];
  172. if (!(port->p_ipend & isrc))
  173. continue;
  174. if (port->p_ihsrc[i] != NULL)
  175. (*port->p_ihsrc[i])(port->p_iharg);
  176. nints++;
  177. }
  178. }
  179. }
  180. return ((nints > 0) ? FILTER_HANDLED : FILTER_STRAY);
  181. }
  182. int
  183. puc_bfe_attach(device_t dev)
  184. {
  185. char buffer[64];
  186. struct puc_bar *bar;
  187. struct puc_port *port;
  188. struct puc_softc *sc;
  189. struct rman *rm;
  190. intptr_t res;
  191. bus_addr_t ofs, start;
  192. bus_size_t size;
  193. bus_space_handle_t bsh;
  194. bus_space_tag_t bst;
  195. int error, idx;
  196. sc = device_get_softc(dev);
  197. for (idx = 0; idx < PUC_PCI_BARS; idx++)
  198. sc->sc_bar[idx].b_rid = -1;
  199. do {
  200. sc->sc_ioport.rm_type = RMAN_ARRAY;
  201. error = rman_init(&sc->sc_ioport);
  202. if (!error) {
  203. sc->sc_iomem.rm_type = RMAN_ARRAY;
  204. error = rman_init(&sc->sc_iomem);
  205. if (!error) {
  206. sc->sc_irq.rm_type = RMAN_ARRAY;
  207. error = rman_init(&sc->sc_irq);
  208. if (!error)
  209. break;
  210. rman_fini(&sc->sc_iomem);
  211. }
  212. rman_fini(&sc->sc_ioport);
  213. }
  214. return (error);
  215. } while (0);
  216. snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
  217. device_get_nameunit(dev));
  218. sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
  219. snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
  220. device_get_nameunit(dev));
  221. sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
  222. snprintf(buffer, sizeof(buffer), "%s port numbers",
  223. device_get_nameunit(dev));
  224. sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
  225. error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
  226. KASSERT(error == 0, ("%s %d", __func__, __LINE__));
  227. sc->sc_nports = (int)res;
  228. sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
  229. M_PUC, M_WAITOK|M_ZERO);
  230. error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
  231. if (error)
  232. goto fail;
  233. error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
  234. if (error)
  235. goto fail;
  236. for (idx = 0; idx < sc->sc_nports; idx++) {
  237. port = &sc->sc_port[idx];
  238. port->p_nr = idx + 1;
  239. error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
  240. if (error)
  241. goto fail;
  242. port->p_type = res;
  243. error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
  244. if (error)
  245. goto fail;
  246. bar = puc_get_bar(sc, res);
  247. if (bar == NULL) {
  248. error = ENXIO;
  249. goto fail;
  250. }
  251. port->p_bar = bar;
  252. start = rman_get_start(bar->b_res);
  253. error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
  254. if (error)
  255. goto fail;
  256. ofs = res;
  257. error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
  258. if (error)
  259. goto fail;
  260. size = res;
  261. rm = (bar->b_type == SYS_RES_IOPORT)
  262. ? &sc->sc_ioport: &sc->sc_iomem;
  263. port->p_rres = rman_reserve_resource(rm, start + ofs,
  264. start + ofs + size - 1, size, 0, NULL);
  265. if (port->p_rres != NULL) {
  266. bsh = rman_get_bushandle(bar->b_res);
  267. bst = rman_get_bustag(bar->b_res);
  268. bus_space_subregion(bst, bsh, ofs, size, &bsh);
  269. rman_set_bushandle(port->p_rres, bsh);
  270. rman_set_bustag(port->p_rres, bst);
  271. }
  272. port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
  273. port->p_nr, 1, 0, NULL);
  274. if (port->p_ires == NULL) {
  275. error = ENXIO;
  276. goto fail;
  277. }
  278. error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
  279. if (error)
  280. goto fail;
  281. port->p_rclk = res;
  282. port->p_dev = device_add_child(dev, NULL, -1);
  283. if (port->p_dev != NULL)
  284. device_set_ivars(port->p_dev, (void *)port);
  285. }
  286. error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
  287. if (error)
  288. goto fail;
  289. sc->sc_ilr = res;
  290. if (bootverbose && sc->sc_ilr != 0)
  291. device_printf(dev, "using interrupt latch register\n");
  292. sc->sc_irid = 0;
  293. sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
  294. RF_ACTIVE|RF_SHAREABLE);
  295. if (sc->sc_ires != NULL) {
  296. error = bus_setup_intr(dev, sc->sc_ires,
  297. INTR_TYPE_TTY, puc_intr, NULL, sc, &sc->sc_icookie);
  298. if (error)
  299. error = bus_setup_intr(dev, sc->sc_ires,
  300. INTR_TYPE_TTY | INTR_MPSAFE, NULL,
  301. (driver_intr_t *)puc_intr, sc, &sc->sc_icookie);
  302. else
  303. sc->sc_fastintr = 1;
  304. if (error) {
  305. device_printf(dev, "could not activate interrupt\n");
  306. bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
  307. sc->sc_ires);
  308. sc->sc_ires = NULL;
  309. }
  310. }
  311. if (sc->sc_ires == NULL) {
  312. /* XXX no interrupt resource. Force polled mode. */
  313. sc->sc_polled = 1;
  314. }
  315. /* Probe and attach our children. */
  316. for (idx = 0; idx < sc->sc_nports; idx++) {
  317. port = &sc->sc_port[idx];
  318. if (port->p_dev == NULL)
  319. continue;
  320. error = device_probe_and_attach(port->p_dev);
  321. if (error) {
  322. device_delete_child(dev, port->p_dev);
  323. port->p_dev = NULL;
  324. }
  325. }
  326. /*
  327. * If there are no serdev devices, then our interrupt handler
  328. * will do nothing. Tear it down.
  329. */
  330. if (sc->sc_serdevs == 0UL)
  331. bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
  332. return (0);
  333. fail:
  334. for (idx = 0; idx < sc->sc_nports; idx++) {
  335. port = &sc->sc_port[idx];
  336. if (port->p_dev != NULL)
  337. device_delete_child(dev, port->p_dev);
  338. if (port->p_rres != NULL)
  339. rman_release_resource(port->p_rres);
  340. if (port->p_ires != NULL)
  341. rman_release_resource(port->p_ires);
  342. }
  343. for (idx = 0; idx < PUC_PCI_BARS; idx++) {
  344. bar = &sc->sc_bar[idx];
  345. if (bar->b_res != NULL)
  346. bus_release_resource(sc->sc_dev, bar->b_type,
  347. bar->b_rid, bar->b_res);
  348. }
  349. rman_fini(&sc->sc_irq);
  350. free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
  351. rman_fini(&sc->sc_iomem);
  352. free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
  353. rman_fini(&sc->sc_ioport);
  354. free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
  355. free(sc->sc_port, M_PUC);
  356. return (error);
  357. }
  358. int
  359. puc_bfe_detach(device_t dev)
  360. {
  361. struct puc_bar *bar;
  362. struct puc_port *port;
  363. struct puc_softc *sc;
  364. int error, idx;
  365. sc = device_get_softc(dev);
  366. /* Detach our children. */
  367. error = 0;
  368. for (idx = 0; idx < sc->sc_nports; idx++) {
  369. port = &sc->sc_port[idx];
  370. if (port->p_dev == NULL)
  371. continue;
  372. if (device_detach(port->p_dev) == 0) {
  373. device_delete_child(dev, port->p_dev);
  374. if (port->p_rres != NULL)
  375. rman_release_resource(port->p_rres);
  376. if (port->p_ires != NULL)
  377. rman_release_resource(port->p_ires);
  378. } else
  379. error = ENXIO;
  380. }
  381. if (error)
  382. return (error);
  383. if (sc->sc_serdevs != 0UL)
  384. bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
  385. bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
  386. for (idx = 0; idx < PUC_PCI_BARS; idx++) {
  387. bar = &sc->sc_bar[idx];
  388. if (bar->b_res != NULL)
  389. bus_release_resource(sc->sc_dev, bar->b_type,
  390. bar->b_rid, bar->b_res);
  391. }
  392. rman_fini(&sc->sc_irq);
  393. free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
  394. rman_fini(&sc->sc_iomem);
  395. free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
  396. rman_fini(&sc->sc_ioport);
  397. free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
  398. free(sc->sc_port, M_PUC);
  399. return (0);
  400. }
  401. int
  402. puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
  403. {
  404. struct puc_softc *sc;
  405. intptr_t res;
  406. int error;
  407. sc = device_get_softc(dev);
  408. sc->sc_dev = dev;
  409. sc->sc_cfg = cfg;
  410. /* We don't attach to single-port serial cards. */
  411. if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
  412. return (EDOOFUS);
  413. error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
  414. if (error)
  415. return (error);
  416. error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
  417. if (error)
  418. return (error);
  419. if (res != 0)
  420. device_set_desc(dev, (const char *)res);
  421. return (BUS_PROBE_DEFAULT);
  422. }
  423. struct resource *
  424. puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
  425. u_long start, u_long end, u_long count, u_int flags)
  426. {
  427. struct puc_port *port;
  428. struct resource *res;
  429. device_t assigned, originator;
  430. int error;
  431. /* Get our immediate child. */
  432. originator = child;
  433. while (child != NULL && device_get_parent(child) != dev)
  434. child = device_get_parent(child);
  435. if (child == NULL)
  436. return (NULL);
  437. port = device_get_ivars(child);
  438. KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
  439. if (rid == NULL || *rid != 0)
  440. return (NULL);
  441. /* We only support default allocations. */
  442. if (start != 0UL || end != ~0UL)
  443. return (NULL);
  444. if (type == port->p_bar->b_type)
  445. res = port->p_rres;
  446. else if (type == SYS_RES_IRQ)
  447. res = port->p_ires;
  448. else
  449. return (NULL);
  450. if (res == NULL)
  451. return (NULL);
  452. assigned = rman_get_device(res);
  453. if (assigned == NULL) /* Not allocated */
  454. rman_set_device(res, originator);
  455. else if (assigned != originator)
  456. return (NULL);
  457. if (flags & RF_ACTIVE) {
  458. error = rman_activate_resource(res);
  459. if (error) {
  460. if (assigned == NULL)
  461. rman_set_device(res, NULL);
  462. return (NULL);
  463. }
  464. }
  465. return (res);
  466. }
  467. int
  468. puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
  469. struct resource *res)
  470. {
  471. struct puc_port *port;
  472. device_t originator;
  473. /* Get our immediate child. */
  474. originator = child;
  475. while (child != NULL && device_get_parent(child) != dev)
  476. child = device_get_parent(child);
  477. if (child == NULL)
  478. return (EINVAL);
  479. port = device_get_ivars(child);
  480. KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
  481. if (rid != 0 || res == NULL)
  482. return (EINVAL);
  483. if (type == port->p_bar->b_type) {
  484. if (res != port->p_rres)
  485. return (EINVAL);
  486. } else if (type == SYS_RES_IRQ) {
  487. if (res != port->p_ires)
  488. return (EINVAL);
  489. if (port->p_hasintr)
  490. return (EBUSY);
  491. } else
  492. return (EINVAL);
  493. if (rman_get_device(res) != originator)
  494. return (ENXIO);
  495. if (rman_get_flags(res) & RF_ACTIVE)
  496. rman_deactivate_resource(res);
  497. rman_set_device(res, NULL);
  498. return (0);
  499. }
  500. int
  501. puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
  502. u_long *startp, u_long *countp)
  503. {
  504. struct puc_port *port;
  505. struct resource *res;
  506. u_long start;
  507. /* Get our immediate child. */
  508. while (child != NULL && device_get_parent(child) != dev)
  509. child = device_get_parent(child);
  510. if (child == NULL)
  511. return (EINVAL);
  512. port = device_get_ivars(child);
  513. KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
  514. if (type == port->p_bar->b_type)
  515. res = port->p_rres;
  516. else if (type == SYS_RES_IRQ)
  517. res = port->p_ires;
  518. else
  519. return (ENXIO);
  520. if (rid != 0 || res == NULL)
  521. return (ENXIO);
  522. start = rman_get_start(res);
  523. if (startp != NULL)
  524. *startp = start;
  525. if (countp != NULL)
  526. *countp = rman_get_end(res) - start + 1;
  527. return (0);
  528. }
  529. int
  530. puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
  531. int flags, driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
  532. {
  533. struct puc_port *port;
  534. struct puc_softc *sc;
  535. device_t originator;
  536. int i, isrc, serdev;
  537. sc = device_get_softc(dev);
  538. /* Get our immediate child. */
  539. originator = child;
  540. while (child != NULL && device_get_parent(child) != dev)
  541. child = device_get_parent(child);
  542. if (child == NULL)
  543. return (EINVAL);
  544. port = device_get_ivars(child);
  545. KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
  546. if (cookiep == NULL || res != port->p_ires)
  547. return (EINVAL);
  548. /* We demand that serdev devices use filter_only interrupts. */
  549. if (ihand != NULL)
  550. return (ENXIO);
  551. if (rman_get_device(port->p_ires) != originator)
  552. return (ENXIO);
  553. /*
  554. * Have non-serdev ports handled by the bus implementation. It
  555. * supports multiple handlers for a single interrupt as it is,
  556. * so we wouldn't add value if we did it ourselves.
  557. */
  558. serdev = 0;
  559. if (port->p_type == PUC_TYPE_SERIAL) {
  560. i = 0, isrc = SER_INT_OVERRUN;
  561. while (i < PUC_ISRCCNT) {
  562. port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
  563. if (port->p_ihsrc[i] != NULL)
  564. serdev = 1;
  565. i++, isrc <<= 1;
  566. }
  567. }
  568. if (!serdev)
  569. return (BUS_SETUP_INTR(device_get_parent(dev), originator,
  570. sc->sc_ires, flags, filt, ihand, arg, cookiep));
  571. sc->sc_serdevs |= 1UL << (port->p_nr - 1);
  572. port->p_hasintr = 1;
  573. port->p_iharg = arg;
  574. *cookiep = port;
  575. return (0);
  576. }
  577. int
  578. puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
  579. void *cookie)
  580. {
  581. struct puc_port *port;
  582. struct puc_softc *sc;
  583. device_t originator;
  584. int i;
  585. sc = device_get_softc(dev);
  586. /* Get our immediate child. */
  587. originator = child;
  588. while (child != NULL && device_get_parent(child) != dev)
  589. child = device_get_parent(child);
  590. if (child == NULL)
  591. return (EINVAL);
  592. port = device_get_ivars(child);
  593. KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
  594. if (res != port->p_ires)
  595. return (EINVAL);
  596. if (rman_get_device(port->p_ires) != originator)
  597. return (ENXIO);
  598. if (!port->p_hasintr)
  599. return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
  600. sc->sc_ires, cookie));
  601. if (cookie != port)
  602. return (EINVAL);
  603. port->p_hasintr = 0;
  604. port->p_iharg = NULL;
  605. for (i = 0; i < PUC_ISRCCNT; i++)
  606. port->p_ihsrc[i] = NULL;
  607. return (0);
  608. }
  609. int
  610. puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
  611. {
  612. struct puc_port *port;
  613. /* Get our immediate child. */
  614. while (child != NULL && device_get_parent(child) != dev)
  615. child = device_get_parent(child);
  616. if (child == NULL)
  617. return (EINVAL);
  618. port = device_get_ivars(child);
  619. KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
  620. if (result == NULL)
  621. return (EINVAL);
  622. switch(index) {
  623. case PUC_IVAR_CLOCK:
  624. *result = port->p_rclk;
  625. break;
  626. case PUC_IVAR_TYPE:
  627. *result = port->p_type;
  628. break;
  629. default:
  630. return (ENOENT);
  631. }
  632. return (0);
  633. }
  634. int
  635. puc_bus_print_child(device_t dev, device_t child)
  636. {
  637. struct puc_port *port;
  638. int retval;
  639. port = device_get_ivars(child);
  640. retval = 0;
  641. retval += bus_print_child_header(dev, child);
  642. retval += printf(" at port %d", port->p_nr);
  643. retval += bus_print_child_footer(dev, child);
  644. return (retval);
  645. }
  646. int
  647. puc_bus_child_location_str(device_t dev, device_t child, char *buf,
  648. size_t buflen)
  649. {
  650. struct puc_port *port;
  651. port = device_get_ivars(child);
  652. snprintf(buf, buflen, "port=%d", port->p_nr);
  653. return (0);
  654. }
  655. int
  656. puc_bus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
  657. size_t buflen)
  658. {
  659. struct puc_port *port;
  660. port = device_get_ivars(child);
  661. snprintf(buf, buflen, "type=%d", port->p_type);
  662. return (0);
  663. }