xref: /freebsd/sys/dev/iicbus/controller/opencores/iicoc_pci.c (revision 160179ea3e24651cd8b15a4fafce519546eac505)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2012 Broadcom Corporation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/rman.h>
37 
38 #include <dev/iicbus/iicbus.h>
39 #include <dev/iicbus/iiconf.h>
40 
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 
44 #include "iicbus_if.h"
45 #include "iicoc.h"
46 
47 static int
iicoc_detach(device_t dev)48 iicoc_detach(device_t dev)
49 {
50 	struct iicoc_softc *sc;
51 
52 	sc = device_get_softc(dev);
53 	bus_generic_detach(dev);
54 	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res);
55 	mtx_destroy(&sc->sc_mtx);
56 
57 	return (0);
58 }
59 
60 /*
61  * We add all the devices which we know about.
62  * The generic attach routine will attach them if they are alive.
63  */
64 static int
iicoc_attach(device_t dev)65 iicoc_attach(device_t dev)
66 {
67 	struct iicoc_softc *sc;
68 
69 	sc = device_get_softc(dev);
70 
71 	sc->dev = dev;
72 	mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF);
73 	sc->mem_rid = 0;
74 	sc->mem_res = bus_alloc_resource_anywhere(dev,
75 	    SYS_RES_MEMORY, &sc->mem_rid, 0x100, RF_ACTIVE);
76 
77 	if (sc->mem_res == NULL) {
78 		device_printf(dev, "Could not allocate bus resource.\n");
79 		return (-1);
80 	}
81 	iicoc_init(dev);
82 	sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
83 	if (sc->iicbus == NULL) {
84 		device_printf(dev, "Could not allocate iicbus instance.\n");
85 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
86 		    sc->mem_res);
87 		mtx_destroy(&sc->sc_mtx);
88 		return (-1);
89 	}
90 	bus_attach_children(dev);
91 
92 	return (0);
93 }
94 
95 static int
iicoc_probe(device_t dev)96 iicoc_probe(device_t dev)
97 {
98 	struct iicoc_softc *sc;
99 
100 	sc = device_get_softc(dev);
101 	if ((pci_get_vendor(dev) == 0x184e) &&
102 	    (pci_get_device(dev) == 0x1011)) {
103 		sc->clockfreq = XLP_I2C_CLKFREQ;
104 		sc->i2cfreq = XLP_I2C_FREQ;
105 		sc->reg_shift = 2;
106 		device_set_desc(dev, "Netlogic XLP I2C Controller");
107 		return (BUS_PROBE_DEFAULT);
108 	}
109 	return (ENXIO);
110 }
111 
112 static device_method_t iicoc_methods[] = {
113 	/* device interface */
114 	DEVMETHOD(device_probe, iicoc_probe),
115 	DEVMETHOD(device_attach, iicoc_attach),
116 	DEVMETHOD(device_detach, iicoc_detach),
117 
118 	/* iicbus interface */
119 	DEVMETHOD(iicbus_callback, iicbus_null_callback),
120 	DEVMETHOD(iicbus_repeated_start, iicoc_iicbus_repeated_start),
121 	DEVMETHOD(iicbus_start, iicoc_iicbus_start),
122 	DEVMETHOD(iicbus_stop, iicoc_iicbus_stop),
123 	DEVMETHOD(iicbus_reset, iicoc_iicbus_reset),
124 	DEVMETHOD(iicbus_write, iicoc_iicbus_write),
125 	DEVMETHOD(iicbus_read, iicoc_iicbus_read),
126 	DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
127 
128 	DEVMETHOD_END
129 };
130 
131 static driver_t iicoc_driver = {
132 	"iicoc",
133 	iicoc_methods,
134 	sizeof(struct iicoc_softc),
135 };
136 
137 DRIVER_MODULE(iicoc, pci, iicoc_driver, 0, 0);
138